我正在尝试使用Yahoo的api创建一个天气应用程序。
不幸的是,我不知道如何引用JSON,虽然我已经搜索了解决方案,但它们并不适用于我。
我正在尝试检索名为forecast的数组:
{
"query": {
"count": 1,
"created": "2016-06-22T16:12:10Z",
"lang": "en-US",
"results": {
"channel": {
"units": {
"distance": "km",
"pressure": "mb",
"speed": "km/h",
"temperature": "C"
},
"title": "Yahoo! Weather - Sydney, NSW, AU",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/",
"description": "Yahoo! Weather for Sydney, NSW, AU",
"language": "en-us",
"lastBuildDate": "Thu, 23 Jun 2016 02:12 AM AEST",
"ttl": "60",
"location": {
"city": "Sydney",
"country": "Australia",
"region": " NSW"
},
"wind": {
"chill": "48",
"direction": "293",
"speed": "17.70"
},
"atmosphere": {
"humidity": "72",
"pressure": "34202.54",
"rising": "0",
"visibility": "25.91"
},
"astronomy": {
"sunrise": "7:1 am",
"sunset": "4:55 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 Sydney, NSW, AU at 01:00 AM AEST",
"lat": "-33.868999",
"long": "151.209045",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/",
"pubDate": "Thu, 23 Jun 2016 01:00 AM AEST",
"condition": {
"code": "31",
"date": "Thu, 23 Jun 2016 01:00 AM AEST",
"temp": "10",
"text": "Clear"
},
"forecast": [{
"code": "34",
"date": "23 Jun 2016",
"day": "Thu",
"high": "16",
"low": "10",
"text": "Mostly Sunny"
}, {
"code": "23",
"date": "24 Jun 2016",
"day": "Fri",
"high": "15",
"low": "10",
"text": "Breezy"
}, {
"code": "23",
"date": "25 Jun 2016",
"day": "Sat",
"high": "13",
"low": "8",
"text": "Breezy"
}, {
"code": "28",
"date": "26 Jun 2016",
"day": "Sun",
"high": "12",
"low": "6",
"text": "Mostly Cloudy"
}, {
"code": "11",
"date": "27 Jun 2016",
"day": "Mon",
"high": "10",
"low": "8",
"text": "Showers"
}, {
"code": "11",
"date": "28 Jun 2016",
"day": "Tue",
"high": "14",
"low": "10",
"text": "Showers"
}, {
"code": "30",
"date": "29 Jun 2016",
"day": "Wed",
"high": "15",
"low": "11",
"text": "Partly Cloudy"
}, {
"code": "34",
"date": "30 Jun 2016",
"day": "Thu",
"high": "16",
"low": "8",
"text": "Mostly Sunny"
}, {
"code": "34",
"date": "01 Jul 2016",
"day": "Fri",
"high": "16",
"low": "11",
"text": "Mostly Sunny"
}, {
"code": "23",
"date": "02 Jul 2016",
"day": "Sat",
"high": "12",
"low": "11",
"text": "Breezy"
}],
"description": "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/31.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Clear\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Thu - Mostly Sunny. High: 16Low: 10\n<BR /> Fri - Breezy. High: 15Low: 10\n<BR /> Sat - Breezy. High: 13Low: 8\n<BR /> Sun - Mostly Cloudy. High: 12Low: 6\n<BR /> Mon - Showers. High: 10Low: 8\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-26198393/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>",
"guid": {
"isPermaLink": "false"
}
}
}
}
}
}
我尝试过这个解决方案:
final String OWM_QUERY = "query";
final String OWM_RESULTS = "results";
final String OWM_CHANNEL = "channel";
final String OWM_ITEM = "item";
final String OWM_FORECAST = "forecast";
final String OWM_MAX = "high";
final String OWM_MIN = "low";
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONObject(OWM_QUERY)
.getJSONObject(OWM_RESULTS)
.getJSONObject(OWM_CHANNEL)
.getJSONObject(OWM_ITEM)
.getJSONArray(OWM_FORECAST);
但这让我回答:
org.json.JSONObject $ 1类型结果的值null无法转换为JSONObject
错误,所以我假设我尝试引用数组的方式不正确。
感谢您提前提供任何帮助
编辑:
完全错误:
06-23 03:02:27.570 18607-18710/com.example.alan.organiseme V/FetchWeatherTask: Forecast string: {"query":{"count":0,"created":"2016-06-22T17:02:08Z","lang":"en-US","results":null}}
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme E/FetchWeatherTask: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
org.json.JSONException: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONObject(JSONObject.java:613)
at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:130)
at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: org.json.JSONException: Value null at results of type org.json.JSONObject$1 cannot be converted to JSONObject
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: at org.json.JSON.typeMismatch(JSON.java:100)
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: at org.json.JSONObject.getJSONObject(JSONObject.java:613)
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:130)
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
06-23 03:02:27.571 18607-18710/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-23 03:02:27.572 18607-18710/com.example.alan.organiseme W/System.err: at java.lang.Thread.run(Thread.java:818)
第二次错误
06-23 03:36:11.704 17086-17172/com.example.alan.organiseme V/FetchWeatherTask: Forecast string: {"query":{"count":1,"created":"2016-06-22T17:35:52Z","lang":"en-US","results":{"channel":{"units":{"distance":"km","pressure":"mb","speed":"km/h","temperature":"C"},"title":"Yahoo! Weather - Sydney, NSW, AU","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/","description":"Yahoo! Weather for Sydney, NSW, AU","language":"en-us","lastBuildDate":"Thu, 23 Jun 2016 03:35 AM AEST","ttl":"60","location":{"city":"Sydney","country":"Australia","region":" NSW"},"wind":{"chill":"45","direction":"338","speed":"11.27"},"atmosphere":{"humidity":"80","pressure":"33965.49","rising":"0","visibility":"25.91"},"astronomy":{"sunrise":"7:1 am","sunset":"4:55 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 Sydney, NSW, AU at 03:00 AM AEST","lat":"-33.783611","long":"151.145325","link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/","pubDate":"Thu, 23 Jun 2016 03:00 AM AEST","condition":{"code":"31","date":"Thu, 23 Jun 2016 03:00 AM AEST","temp":"8","text":"Clear"},"forecast":[{"code":"30","date":"23 Jun 2016","day":"Thu","high":"15","low":"6","text":"Partly Cloudy"},{"code":"32","date":"24 Jun 2016","day":"Fri","high":"14","low":"7","text":"Sunny"},{"code":"32","date":"25 Jun 2016","day":"Sat","high":"12","low":"5","text":"Sunny"},{"code":"28","date":"26 Jun 2016","day":"Sun","high":"11","low":"3","text":"Mostly Cloudy"},{"code":"39","date":"27 Jun 2016","day":"Mon","high":"8","low":"6","text":"Scattered Showers"},{"code":"39","date":"28 Jun 2016","day":"Tue","high":"12","low":"6","text":"Scattered Showers"},{"code":"30","date":"29 Jun 2016","day":"Wed","high":"14","low":"7","text":"Partly Cloudy"},{"code":"34","date":"30 Jun 2016","day":"Thu","high":"15","low":"6","text":"Mostly Sunny"},{"code":"34","date":"01 Jul 2016","day":"Fri","high":"15","low":"8","text":"Mostly Sunny"},{"code":"34","date":"02 Jul 2016","day":"Sat","high":"12","low":"10","text":"Mostly Sunny"}],"description":"<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/31.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Clear\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Thu - Partly Cloudy. High: 15Low: 6\n<BR /> Fri - Sunny. High: 14Low: 7\n<BR /> Sat - Sunny. High: 12Low: 5\n<BR /> Sun - Mostly Cloudy. High: 11Low: 3\n<BR /> Mon - Scattered Showers. High: 8Low: 6\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-55857444/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>","guid":{"isPermaLink":"false"}}}}}}
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme E/FetchWeatherTask: No value for forecast
org.json.JSONException: No value for forecast
at org.json.JSONObject.get(JSONObject.java:389)
at org.json.JSONObject.getJSONArray(JSONObject.java:584)
at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:168)
at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: org.json.JSONException: No value for forecast
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at org.json.JSONObject.getJSONArray(JSONObject.java:584)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.getWeatherDataFromJson(weather.java:168)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:274)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at com.example.alan.organiseme.weather$FetchWeatherTask.doInBackground(weather.java:78)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
06-23 03:36:11.730 17086-17172/com.example.alan.organiseme W/System.err: at java.lang.Thread.run(Thread.java:818)