WU: How to take elements from a JSONArray that has no name

I am working with an API Weather Underground

and I am trying to map hourly_forecast

to Android

.

For other pages, it was easier, as for the 10day forecast:

"forecast":{
    "txt_forecast": {
    "date":"9:47 AM PDT",
    "forecastday": [
    {
    "period":0,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Thursday",
    "fcttext":"Sun and clouds mixed. Slight chance of a rain shower. High 78F. Winds WSW at 5 to 10 mph.",
    "fcttext_metric":"Partly cloudy. Slight chance of a rain shower. High 26C. Winds WSW at 10 to 15 km/h.",
    "pop":"20"
    }
    ,
    {
    "period":1,
    "icon":"nt_partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
    "title":"Thursday Night",
    "fcttext":"Clear this evening then becoming cloudy after midnight. Low near 65F. Winds WSW at 5 to 10 mph.",
    "fcttext_metric":"Clear early followed by cloudy skies overnight. Low 18C. Winds WSW at 10 to 15 km/h.",
    "pop":"0"
    }
    ,
    {
    "period":2,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Friday",
    "fcttext":"Mostly cloudy early, then afternoon sunshine. High 76F. Winds WSW at 5 to 10 mph.",
    "fcttext_metric":"Cloudy early with partial sunshine expected late. High near 25C. Winds WSW at 10 to 15 km/h.",
    "pop":"0"
    }
    ,
    {
    "period":3,
    "icon":"nt_mostlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
    "title":"Friday Night",
    "fcttext":"Partly cloudy during the evening followed by cloudy skies overnight. Low 64F. Winds WSW at 5 to 10 mph.",
    "fcttext_metric":"Partly cloudy skies early will become overcast later during the night. Low 18C. Winds WSW at 10 to 15 km/h.",
    "pop":"10"
    }
    ,
    {
    "period":4,
    "icon":"partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "title":"Saturday",
    "fcttext":"Cloudy early, becoming mostly sunny in the afternoon. High around 75F. Winds SW at 5 to 10 mph.",
    "fcttext_metric":"Cloudy early, becoming mostly sunny in the afternoon. High 24C. Winds SW at 10 to 15 km/h.",
    "pop":"10"
    }
    ,
    {
    "period":5,
    "icon":"nt_partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
    "title":"Saturday Night",
    "fcttext":"Clear skies early will become overcast later during the night. Low 64F. Winds SW at 5 to 10 mph.",
    "fcttext_metric":"Clear skies in the evening then becoming cloudy overnight. Low 18C. Winds SW at 10 to 15 km/h.",
    "pop":"10"
    }
    ,

    .
    .
    .

    {
    "period":19,
    "icon":"nt_partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
    "title":"Saturday Night",
    "fcttext":"Mostly clear during the evening followed by mostly cloudy skies overnight. Low near 65F. Winds SW at 5 to 10 mph.",
    "fcttext_metric":"Mostly clear during the evening followed by mostly cloudy skies overnight. Low 18C. Winds SW at 10 to 15 km/h.",
    "pop":"0"
    }
    ]
    },

      

As you can see, they are divisible by period s, so each element from each period is easy to accept.

In hourly_forecast the json page is different, they are not separated by period and I don't know how to get them:

"hourly_forecast": [
    {
    "FCTTIME": {
    "hour": "12","hour_padded": "12","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "2","mday_padded": "02","yday": "182","isdst": "1","epoch": "1435863600","pretty": "12:00 PM PDT on July 02, 2015","civil": "12:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Thursday","weekday_name_night": "Thursday Night","weekday_name_abbrev": "Thu","weekday_name_unlang": "Thursday","weekday_name_night_unlang": "Thursday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
    },
    "temp": {"english": "76", "metric": "24"},
    "dewpoint": {"english": "62", "metric": "17"},
    "condition": "Partly Cloudy",
    "icon": "partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "fctcode": "2",
    "sky": "46",
    "wspd": {"english": "7", "metric": "11"},
    "wdir": {"dir": "WSW", "degrees": "251"},
    "wx": "Partly Cloudy",
    "uvi": "10",
    "humidity": "62",
    "windchill": {"english": "-9999", "metric": "-9999"},
    "heatindex": {"english": "-9999", "metric": "-9999"},
    "feelslike": {"english": "76", "metric": "24"},
    "qpf": {"english": "0.0", "metric": "0"},
    "snow": {"english": "0.0", "metric": "0"},
    "pop": "20",
    "mslp": {"english": "29.94", "metric": "1014"}
    }
    ,
    {
    "FCTTIME": {
    "hour": "13","hour_padded": "13","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "2","mday_padded": "02","yday": "182","isdst": "1","epoch": "1435867200","pretty": "1:00 PM PDT on July 02, 2015","civil": "1:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Thursday","weekday_name_night": "Thursday Night","weekday_name_abbrev": "Thu","weekday_name_unlang": "Thursday","weekday_name_night_unlang": "Thursday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
    },
    "temp": {"english": "77", "metric": "25"},
    "dewpoint": {"english": "62", "metric": "17"},
    "condition": "Partly Cloudy",
    "icon": "partlycloudy",
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
    "fctcode": "2",
    "sky": "37",
    "wspd": {"english": "8", "metric": "13"},
    "wdir": {"dir": "WSW", "degrees": "253"},
    "wx": "Partly Cloudy",
    "uvi": "11",
    "humidity": "60",
    "windchill": {"english": "-9999", "metric": "-9999"},
    "heatindex": {"english": "-9999", "metric": "-9999"},
    "feelslike": {"english": "77", "metric": "25"},
    "qpf": {"english": "0.0", "metric": "0"},
    "snow": {"english": "0.0", "metric": "0"},
    "pop": "20",
    "mslp": {"english": "29.94", "metric": "1014"}
    }
    ,
    {
    "FCTTIME": {
    "hour": "14","hour_padded": "14","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "2","mday_padded": "02","yday": "182","isdst": "1","epoch": "1435870800","pretty": "2:00 PM PDT on July 02, 2015","civil": "2:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Thursday","weekday_name_night": "Thursday Night","weekday_name_abbrev": "Thu","weekday_name_unlang": "Thursday","weekday_name_night_unlang": "Thursday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
    },
    "temp": {"english": "79", "metric": "26"},
    "dewpoint": {"english": "62", "metric": "17"},
    "condition": "Clear",
    "icon": "clear",
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
    "fctcode": "1",
    "sky": "19",
    "wspd": {"english": "9", "metric": "14"},
    "wdir": {"dir": "WSW", "degrees": "254"},
    "wx": "Sunny",
    "uvi": "10",
    "humidity": "57",
    "windchill": {"english": "-9999", "metric": "-9999"},
    "heatindex": {"english": "-9999", "metric": "-9999"},
    "feelslike": {"english": "79", "metric": "26"},
    "qpf": {"english": "0.0", "metric": "0"},
    "snow": {"english": "0.0", "metric": "0"},
    "pop": "0",
    "mslp": {"english": "29.93", "metric": "1014"}
    }
    ,

    .
    .
    .
    .

      

Edit

I would like to show the text of the elements separately, in different ones TextViews

, not TextView

which each element displays

+3


source to share


3 answers


Here's how you get it:

JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
JSONArray array = jsonObject.getJSONArray("hourly_forcast");
JSONObject object = array.getJSONObject(0); //GET FIRST OBJECT
JSONObject objectFCTTIME = object.getJSONObject("FCTTIME");
int hour = objectFCTTIME.getInt("hour");

      



To get the second one, just do the following:

JSONObject object = array.getJSONObject(1); //GET SECOND OBJECT
JSONObject objectFCTTIME = object.getJSONObject("FCTTIME");
int hour = objectFCTTIME.getInt("hour");

      

+1


source


You have a structure that you can go to:

hourly_forecast is an array and each child has attributes:



"FCTTIME": {
"hour": "12","hour_padded": "12","min": "00","min_unpadded": "0","sec": "0","year": "2015","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "2","mday_padded": "02","yday": "182","isdst": "1","epoch": "1435863600","pretty": "12:00 PM PDT on July 02, 2015","civil": "12:00 PM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Thursday","weekday_name_night": "Thursday Night","weekday_name_abbrev": "Thu","weekday_name_unlang": "Thursday","weekday_name_night_unlang": "Thursday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "76", "metric": "24"},
"dewpoint": {"english": "62", "metric": "17"},
"condition": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"fctcode": "2",
"sky": "46",
"wspd": {"english": "7", "metric": "11"},
"wdir": {"dir": "WSW", "degrees": "251"},
"wx": "Partly Cloudy",
"uvi": "10",
"humidity": "62",
"windchill": {"english": "-9999", "metric": "-9999"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "76", "metric": "24"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "20",
"mslp": {"english": "29.94", "metric": "1014"}

      

What is the problem?

+1


source


Why not use a for loop and iterate over all JsonObjects in the array? Use array.length()

to get the size and array.get(index)

to get each item.

JSONObject jsonObject = new JSONObject(YOUR_JSON_STRING);
JSONArray array = jsonObject.getJSONArray("hourly_forcast");
int count = array.length();
for(int i = 0; i < count; i++){
  //here you get each item...
  JsonObject item = (JsonObject) array.get(i);
}

      

0


source







All Articles