JSONException:索引5超出范围[0..5)

时间:2014-12-03 22:11:47

标签: android json jsonexception

我现在正在开发使用此JSON对象的应用程序时出现问题:http://api.worldweatheronline.com/premium/v1/marine.ashx?q=-34.8799074,174.7565664&key=3477e975c4d9a8a0eaac2b2c818f4&format=json&tide=yes

有些日子它工作正常,从JSON对象中获取数据没有问题,有些日子我遇到这种错误:JSONException:索引5超出范围[0..5)。

12-04 11:12:30.328    1916-1931/android_kaizen.com.drawerskippermate E/FetchWeatherTask﹕ Index 5 out of range [0..5)
org.json.JSONException: Index 5 out of range [0..5)
        at org.json.JSONArray.get(JSONArray.java:263)
        at org.json.JSONArray.getJSONObject(JSONArray.java:480)
        at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.getWeatherDataFromJson(MainActivity.java:579)
        at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:754)
        at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:318)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:864)

甚至没有改变一行代码!! 奇怪的是,可以在Web浏览器中看到JSON对象。

有没有人遇到过类似的情况,可以解释为什么会这样?

THX!

编辑:这是我的代码:

private DayForecast[] getWeatherDataFromJson(String forecastJsonStr) throws JSONException {

        final String JSON_DATA = "data";
        final String JSON_WEATHER = "weather";
        final String JSON_MAX = "maxtempC";
        final String JSON_MIN = "mintempC";
        final String JSON_DATE = "date";
        final String JSON_ARRAY_HOURLY = "hourly";
        final String JSON_ARRAY_ASTRONOMY = "astronomy";
        final String JSON_ARRAY_TIDES = "tides";
        final int JSON_OBJECT_MORNING = 3;
        final int JSON_OBJECT_MIDDAY = 4;
        final int JSON_OBJECT_AFTERNOON = 5;
        final String JSON_DESCRIPTION = "weatherDesc";


        // WeatherCode and description at midday
        final String JSON_WEATHER_CODE = "weatherCode";
        final String JSON_WEATHER_DESCRIPTION = "weatherDesc";

        final String JSON_TEMP = "tempC";
        final String JSON_PRECIPITATION = "precipMM";
        final String JSON_CLOUD_COVER = "cloudcover";
        final String JSON_VISIBILITY = "visibility";
        final String JSON_SUNRISE = "sunrise";
        final String JSON_SUNSET = "sunset";
        final String JSON_PRESSURE = "pressure";
        final String JSON_WIND_DIRECTION = "winddir16Point";
        final String JSON_WIND_SPEED = "windspeedKmph";
        final String JSON_WIND_GUST = "WindGustKmph";
        final String JSON_WATER_TEMP = "waterTemp_C";
        final String JSON_SWELL_DIRECTION = "swellDir16Point";
        final String JSON_SWELL_HEIGHT = "swellHeight_m";
        final String JSON_TIDE = "tideTime";


        JSONObject forecastJson = new JSONObject(forecastJsonStr);
        JSONArray weatherArray = forecastJson.getJSONObject(JSON_DATA).getJSONArray(JSON_WEATHER);

        int numDays = weatherArray.length();

        mDayForecastArray = new DayForecast[numDays];


        for (int i = 0; i < weatherArray.length(); i++) {

            String day;
            String description;
            String highAndLow;





            JSONObject dayForecast = weatherArray.getJSONObject(i);

            String dateTime = dayForecast.getString(JSON_DATE);
            day = getReadableDateString(dateTime);

            // Get the right JsonObject for different time of the day
            JSONObject morningObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MORNING);
            JSONObject midDayObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MIDDAY);
            JSONObject afternoonObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_AFTERNOON);

            /**
             * Common Conditions.
             */
            // Parse the two midday values from Json to variables


            /**
             * Morning Conditions.
             */


            description = midDayObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value");

            int high = dayForecast.getInt(JSON_MAX);
            int low = dayForecast.getInt(JSON_MIN);
            highAndLow = formatHighLows(high, low);


            DayForecast dayForecastObject = new DayForecast();
            dayForecastObject.setmDate(day);

            dayForecastObject.setmDescription(description);


            dayForecastObject.setmWeatherCode(afternoonObject.getInt(JSON_WEATHER_CODE));
            dayForecastObject.setmWeatherDescription(afternoonObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value"));
            dayForecastObject.setmTempMax(convertCelsiusToFahrenheit(dayForecast.getInt(JSON_MAX)));
            dayForecastObject.setmTempMin(dayForecast.getInt(JSON_MIN));
            /**
             * Morning Conditions.
             */
            dayForecastObject.setmPrecipitationAM(convertmillimetersToInches(morningObject.getInt(JSON_PRECIPITATION)));
            dayForecastObject.setmCloudCoverAM(morningObject.getInt(JSON_CLOUD_COVER));
            dayForecastObject.setmVisibilityAM(convertKilometersToMiles(morningObject.getInt(JSON_VISIBILITY)));
            dayForecastObject.setmSunrise(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNRISE));

            dayForecastObject.setmPressureAM(morningObject.getInt(JSON_PRESSURE));
            dayForecastObject.setmWindSpeedAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_SPEED)));
            dayForecastObject.setmWindGustAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_GUST)));
            dayForecastObject.setmWindDirectionAM(morningObject.getString(JSON_WIND_DIRECTION));

            dayForecastObject.setmWaterTempAM(convertCelsiusToFahrenheit(morningObject.getInt(JSON_WATER_TEMP)));
            dayForecastObject.setmSwellHeightAM(convertMetersToFeet(Float.parseFloat(morningObject.getString(JSON_SWELL_HEIGHT))));
            dayForecastObject.setmSwellDirectionAM(morningObject.getString(JSON_SWELL_DIRECTION));
            dayForecastObject.setmTide1AM(getReadableTide(dayForecast, 0));
            dayForecastObject.setmTide2AM(getReadableTide(dayForecast, 1));

            /**
             * Afternoon Conditions.
             */
            dayForecastObject.setmPrecipitationPM(convertmillimetersToInches(afternoonObject.getInt(JSON_PRECIPITATION)));
            dayForecastObject.setmCloudCoverPM(afternoonObject.getInt(JSON_CLOUD_COVER));
            dayForecastObject.setmVisibilityPM(convertKilometersToMiles(afternoonObject.getInt(JSON_VISIBILITY)));
            dayForecastObject.setmSunset(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNSET));

            dayForecastObject.setmPressurePM(afternoonObject.getInt(JSON_PRESSURE));
            dayForecastObject.setmWindSpeedPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_SPEED)));
            dayForecastObject.setmWindGustPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_GUST)));
            dayForecastObject.setmWindDirectionPM(afternoonObject.getString(JSON_WIND_DIRECTION));

            dayForecastObject.setmWaterTempPM(convertCelsiusToFahrenheit(afternoonObject.getInt(JSON_WATER_TEMP)));
            dayForecastObject.setmSwellHeightPM(convertMetersToFeet(Float.parseFloat(afternoonObject.getString(JSON_SWELL_HEIGHT))));
            dayForecastObject.setmSwellDirectionPM(afternoonObject.getString(JSON_SWELL_DIRECTION));
            dayForecastObject.setmTide1PM(getReadableTide(dayForecast, 2));
            dayForecastObject.setmTide2PM(getReadableTide(dayForecast, 3));


            mDayForecastArray[i] = dayForecastObject;
        }
        for (DayForecast s : mDayForecastArray) {
            Log.v(LOG_TAG, "Forecast entry: " + s.getmDescription() + " / " + String.valueOf(s.getmWeatherCode()));
        }

        return mDayForecastArray;

    }

1 个答案:

答案 0 :(得分:10)

我很确定你有类似的问题: JSONArray Exception : Index 50 out of range [0..50). Is there any limit on JsonArray

.json响应中的对象数量每天都会改变,你必须定义边界(results.length()):

见这个例子:

JSONArray results = jsonResponse.getJSONArray("results");
final int numberOfItemsInResp = results.length();
for (int i = 0; i < numberOfItemsInResp; i++){
    JSONObject perResult = results.getJSONObject(i);
}