JSONArray与JSONObject

时间:2012-11-25 17:28:07

标签: android json api parsing

为什么我的JSONArray作为JSONObject返回?或许我错了。我正在尝试解析位于here的JSON中的一些数据。我读过以前关于这个主题的SO问题(例如问题Here,我学到的是正确区分JSONArray和JSONObject很重要。

它看起来像是两者,更具体地说,它看起来像JSONArray的JSONA,每个都包含名称:值对。我正在尝试“深入挖掘我的数据”,就像建议的答案一样,但我在第96行得到了一个JSONException,就是这个。我想也许“数据”不是数组的正确名称,但只有在我使用该字符串时才会在LogCat中打印JSON。任何帮助都会非常感谢大家。

第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;
}

}

logcat的:

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)

1 个答案:

答案 0 :(得分:2)

它不是一个数组,而是一个JSON对象,由一个名为weather的数组组成。这应该可以解决问题:

weatherData = json.getJSONObject("data").getJSONArray("weather");