Android 解析雅虎对天气预报的API响应
我正在使用创建一个天气应用程序。我想知道天气预报 我尝试过使用JSONObject: 在JSON响应中,预测嵌套在查询>结果>项目>预测下。使用getJSONArrayforcast,您试图从根级别获取它,这是错误的Android 解析雅虎对天气预报的API响应,android,json,yahoo-api,Android,Json,Yahoo Api,我正在使用创建一个天气应用程序。我想知道天气预报 我尝试过使用JSONObject: 在JSON响应中,预测嵌套在查询>结果>项目>预测下。使用getJSONArrayforcast,您试图从根级别获取它,这是错误的 您需要遍历树以获得嵌套的forecast节点。看一看,它解决了同样的问题。您能添加您得到的错误吗?有一个可以上传JSON并获得相应POJO模式的窗口。。然后,您可以使用像Gson这样的库,用几行代码将Yahoo的响应转换为Pojo。此外,如果你使用的是图书馆式的改造,那么这将是小菜
您需要遍历树以获得嵌套的forecast节点。看一看,它解决了同样的问题。您能添加您得到的错误吗?有一个可以上传JSON并获得相应POJO模式的窗口。。然后,您可以使用像Gson这样的库,用几行代码将Yahoo的响应转换为Pojo。此外,如果你使用的是图书馆式的改造,那么这将是小菜一碟。@angelina帮助其他人,如果解决了你的问题,则将答案标记为已接受,如果是重复的问题,则关闭该问题。
JSONObject jobj2 = new JSONObject(response);
JSONArray jsonArray2 = jobj2.getJSONArray("forcast");
for (int i = 0; i < jsonArray2.length(); i++) {
....
}
{
"query": {
"count": 1,
"created": "2018-04-07T05:03:39Z",
"lang": "en-us",
"results": {
"channel": {
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"title": "Yahoo! Weather - London, England, GB",
"link": "...",
"description": "Yahoo! Weather for London, England, GB",
"language": "en-us",
"lastBuildDate": "Sat, 07 Apr 2018 06:03 AM BST",
"ttl": "60",
"location": {
"city": "London",
"country": "United Kingdom",
"region": " England"
},
"wind": {
"chill": "48",
"direction": "90",
"speed": "7"
},
"atmosphere": {
"humidity": "78",
"pressure": "1002.0",
"rising": "0",
"visibility": "16.1"
},
"astronomy": {
"sunrise": "6:21 am",
"sunset": "7:45 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for London, England, GB at 05:00 AM BST",
"lat": "51.506401",
"long": "-0.12721",
"link": "...",
"pubDate": "Sat, 07 Apr 2018 05:00 AM BST",
"condition": {
"code": "26",
"date": "Sat, 07 Apr 2018 05:00 AM BST",
"temp": "49",
"text": "Cloudy"
},
"forecast": [
{
"code": "26",
"date": "07 Apr 2018",
"day": "Sat",
"high": "60",
"low": "50",
"text": "Cloudy"
},
{
"code": "12",
"date": "08 Apr 2018",
"day": "Sun",
"high": "53",
"low": "48",
"text": "Rain"
}
],
"description": "...",
"guid": {
"isPermaLink": "false"
}
}
}
}
}
}