2

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)
4

1 に答える 1

2

これは配列ではなく、weatherという配列で構成されるJSONオブジェクトです。これでうまくいくはずです:

weatherData = json.getJSONObject("data").getJSONArray("weather");
于 2012-11-25T17:34:33.177 に答える