WU:如何从没有名称的

时间:2015-07-02 18:28:49

标签: android json weather-api

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

对于其他页面,事情变得更容易,就像对于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"
    }
    ]
    },

正如您所看到的那样,它们被划分为期间,因此每个期间的每个项目都很容易被采用。

在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中分别显示项目的文字

3 个答案:

答案 0 :(得分:1)

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

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

你有什么问题?

答案 1 :(得分:1)

这是你应该得到的:

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

答案 2 :(得分:0)

为什么不使用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);
}