为什么我的 JSONArray 作为 JSONObject 返回?或者也许我错了。我正在尝试从位于此处的 JSON 解析一些数据。我阅读了有关此主题的先前 SO 问题(例如问题问在这里,我学到的是正确区分 JSONArray 和 JSONObject 很重要。
在我看来,两者都是,更具体地说,它看起来像 JSONObjects 的 JSONArray,每个都包含名称:值对。我正在尝试“深入挖掘以获取我的数据”,就像建议的答案一样,但是我在第 96 行得到了 JSONException,就是这个。我想也许“数据”不是数组的正确名称,但 JSON 仅在我使用该字符串时才会打印在 LogCat 中。任何帮助将不胜感激。
第 96 行 weatherData = json.getJSONArray("data");
我在下面粘贴了相关代码。
public String getWeatherData() {
String url = "http://free.worldweatheronline.com/feed/weather.ashx?q="
+ zipCode
+ "&format=json&num_of_days=5&key=e8570995";
// Create a JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON String from URL
JSONObject json = jParser.getJSONFromUrl(url);
// Log.e("WeatherProvider", "Got JSON from URL");
/*
* try { currentCond = json.getString("data"); } catch (JSONException
* e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
*/
try {
// Get array of weather data
weatherData = json.getJSONArray("data");
Log.e("WeatherProvider", "JSONArray is not null");
if (weatherData != null) {
// loop through the data array
for (int i = 0; i < weatherData.length(); i++) {
JSONArray innerJSONArray = weatherData.getJSONArray(i);
JSONObject object = innerJSONArray.getJSONObject(i);
// Store each json item in a variable
currentCond = object.getString("value");
Log.e("WeatherProvider", "current conditin is "
+ currentCond);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return currentCond;
}
}
日志猫:
11-25 12:03:33.439: W/System.err(4502): org.json.JSONException: Value {"weather": [{"windspeedMiles":"19","winddirection":"W","date":"2012-11-25","precipMM":"0.0","winddirDe gree":"280","winddir16Point":"W","weatherIconUrl":[{"value":"http:\/ \/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"}],"tempMinC":"4","windspeedKmph":"31","tempMaxC":"7","weatherCode":"113","tempMaxF":"44","weatherDesc":[{"value":"Sunny"}],"tempMinF":"39"},{"windspeedMiles":"13","winddirection":"WNW","date":"2012-11-26","precipMM":"0.0","winddirDegree":"290","winddir16Point":"WNW","weatherIconUrl":[{"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"}],"tempMinC":"4","windspeedKmph":"21","tempMaxC":"8","weatherCode":"113","tempMaxF":"46","weatherDesc":[{"value":"Sunny"}],"tempMinF":"40"},{"windspeedMiles":"13","winddirection":"NW","date":"2012-11-27","precipMM":"4.5","winddirDegree":"320","winddir16Point":"NW","weatherIconUrl":[{"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png"}],"tempMinC":"0","windspeedKmph":"21","tempMaxC":"8","weatherCode":"296","tempMaxF":"47","weatherDesc":[{"value":"Light rain"}],"tempMinF":"32"},{"windspeedMiles":"13","winddirection":"WNW","date":"2012-11-28","precipMM":"0.0","winddirDegree":"300","winddir16Point":"WNW","weatherIconUrl":[{"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"}],"tempMinC":"1","windspeedKmph":"21","tempMaxC":"7","weatherCode":"113","tempMaxF":"44","weatherDesc":[{"value":"Sunny"}],"tempMinF":"33"},{"windspeedMiles":"15","winddirection":"WNW","date":"2012-11-29","precipMM":"0.0","winddirDegree":"286","winddir16Point":"WNW","weatherIconUrl":[{"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"}],"tempMinC":"2","windspeedKmph":"23","tempMaxC":"4","weatherCode":"113","tempMaxF":"40","weatherDesc":[{"value":"Sunny"}],"tempMinF":"35"}],"current_condition":[{"observation_time":"04:33 PM","cloudcover":"75","pressure":"1013","visibility":"16","temp_C":"4","temp_F":"39","windspeedMiles":"19","precipMM":"0.0","winddirDegree":"270","winddir16Point":"W","weatherIconUrl":[{"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"}],"humidity":"48","windspeedKmph":"30","weatherCode":"116","weatherDesc":[{"value":"Partly Cloudy"}]}],"request":[{"type":"Zipcode","query":"11212"}]} at data of type org.json.JSONObject cannot be converted to JSONArray
11-25 12:03:33.449: W/System.err(4502): at org.json.JSON.typeMismatch(JSON.java:96)
11-25 12:03:33.449: W/System.err(4502): at org.json.JSONObject.getJSONArray(JSONObject.java:548)
11-25 12:03:33.449: W/System.err(4502): at com.brightr.weathermate.providers.WeatherProvider.getWeatherData(WeatherProvider.java:90)
11-25 12:03:33.449: W/System.err(4502): at com.brightr.weathermate.activities.MainActivity.onClick(MainActivity.java:49)
11-25 12:03:33.449: W/System.err(4502): at android.view.View.performClick(View.java:2532)
11-25 12:03:33.449: W/System.err(4502): at android.view.View$PerformClick.run(View.java:9293)
11-25 12:03:33.449: W/System.err(4502): at android.os.Handler.handleCallback(Handler.java:587)
11-25 12:03:33.449: W/System.err(4502): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 12:03:33.449: W/System.err(4502): at android.os.Looper.loop(Looper.java:150)
11-25 12:03:33.449: W/System.err(4502): at android.app.ActivityThread.main(ActivityThread.java:4263)
11-25 12:03:33.449: W/System.err(4502): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 12:03:33.449: W/System.err(4502): at java.lang.reflect.Method.invoke(Method.java:507)
11-25 12:03:33.449: W/System.err(4502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-25 12:03:33.449: W/System.err(4502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-25 12:03:33.449: W/System.err(4502): at dalvik.system.NativeStart.main(Native Method)