我正在使用 Wunderground api 获取 Android 应用程序的每小时天气预报。我想接收诸如温度和日期之类的信息。
Json 的一个例子是:
"hourly_forecast": [
{
"FCTTIME": {
"hour": "11","hour_padded": "11","min": "00","min_unpadded": "0","sec": "0","year": "2014","mon": "7","mon_padded": "07","mon_abbrev": "Jul","mday": "8","mday_padded": "08","yday": "188","isdst": "1","epoch": "1404842400","pretty": "11:00 AM PDT on July 08, 2014","civil": "11:00 AM","month_name": "July","month_name_abbrev": "Jul","weekday_name": "Tuesday","weekday_name_night": "Tuesday Night","weekday_name_abbrev": "Tue","weekday_name_unlang": "Tuesday","weekday_name_night_unlang": "Tuesday Night","ampm": "AM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "61", "metric": "16"},
"dewpoint": {"english": "56", "metric": "13"},
"condition": "Partly Cloudy",
"icon": "partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"fctcode": "2",
"sky": "58",
"wspd": {"english": "7", "metric": "11"},
"wdir": {"dir": "SW", "degrees": "232"},
"wx": "Partly Cloudy",
"uvi": "9",
"humidity": "83",
"windchill": {"english": "-9999", "metric": "-9999"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "61", "metric": "16"},
"qpf": {"english": "0", "metric": "0"},
"snow": {"english": "0", "metric": "0"},
"pop": "2",
"mslp": {"english": "29.92", "metric": "1013"}
}
它似乎在一个数组中,但是当我尝试以下操作时,它出现了一个不是 json 对象的错误。
String row = rootArray.getAsJsonObject().get("sky").getAsString();
** 编辑 ** 我能够让它与代码一起工作
String skyText = rootobj.getAsJsonObject().getAsJsonArray("hourly_forecast").get(0).getAsJsonObject().get("sky").getAsString();
rootobj 是我的 json 对象