WU:如何从没有名称的 JSONArray 中获取项目

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

我正在使用 Weather Underground API,我正在尝试在 Android 中显示 hourly_forecast

对于其他页面,事情更容易,就像 forecast10day:

"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"
    }
    ]
    },

如你所见,它们被分为 period 秒,因此每个时期的每个项目都很容易拿走。

hourly_forecast和json页面不一样,不分期,不知道怎么取:

"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"}
    }
    ,

    .
    .
    .
    .

编辑

我想单独显示项目的文本,在不同的 TextViews 而不是显示每个项目的 TextView

您有一个可以导航到的结构:

hourly_forecast是一个数组,每个child都有属性:

"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"}

你有什么问题?

这是你应该如何获得它:

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");

要获得第二个,只需执行以下操作:

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

为什么不用for循环遍历数组中的所有JsonObject? 使用 array.length() 获取尺寸,使用 array.get(index) 获取每个项目。

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);
}