Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JSONException:索引5超出范围[0..5]_Android_Json_Jsonexception - Fatal编程技术网

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

Android JSONException:索引5超出范围[0..5],android,json,jsonexception,Android,Json,Jsonexception,我现在正在开发的一个应用程序使用这个JSON对象时遇到了一些问题: 有时它工作得很好,从JSON对象获取数据时没有问题,有时我会遇到这样的错误:JSONException:Index 5超出范围[0..5] 甚至不改变一行代码!! 奇怪的是,在Web浏览器中可以看到JSON对象 有没有人经历过类似的情况,并能解释为什么会发生这种情况 谢谢 编辑:这是我的代码: private DayForecast[] getWeatherDataFromJson(String forecastJsonStr)

我现在正在开发的一个应用程序使用这个JSON对象时遇到了一些问题:

有时它工作得很好,从JSON对象获取数据时没有问题,有时我会遇到这样的错误:JSONException:Index 5超出范围[0..5]

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

有没有人经历过类似的情况,并能解释为什么会发生这种情况

谢谢

编辑:这是我的代码:

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;

    }
private DayForecast[]getWeatherDataFromJson(String forecastJsonStr)抛出JSONException{
最终字符串JSON_DATA=“DATA”;
最终字符串JSON_WEATHER=“WEATHER”;
最后一个字符串JSON_MAX=“maxtempC”;
最后一个字符串JSON_MIN=“mintempC”;
最终字符串JSON_DATE=“DATE”;
最终字符串JSON_ARRAY_HOURLY=“HOURLY”;
最终字符串JSON_ARRAY_ASTRONOMY=“ASTRONOMY”;
最后一个字符串JSON_ARRAY_TIDES=“TIDES”;
最终int JSON_OBJECT_MORNING=3;
最终int JSON_对象_MIDDAY=4;
最终整型JSON_对象_下午=5;
最终字符串JSON_DESCRIPTION=“weatherDesc”;
//中午天气代码和说明
最终字符串JSON_WEATHER_CODE=“weatherCode”;
最终字符串JSON_WEATHER_DESCRIPTION=“weatherDesc”;
最后一个字符串JSON_TEMP=“tempC”;
最终字符串JSON_=“precipMM”;
最终字符串JSON\u CLOUD\u COVER=“cloudcover”;
最终字符串JSON_VISIBILITY=“VISIBILITY”;
最后一个字符串JSON_SUNRISE=“SUNRISE”;
最终字符串JSON_SUNSET=“日落”;
最终管柱JSON_PRESSURE=“PRESSURE”;
最终字符串JSON\u WIND\u DIRECTION=“winddir16Point”;
最终字符串JSON_WIND_SPEED=“windspeedKmph”;
最终字符串JSON\u WIND\u GUST=“WindGustKmph”;
最后一个字符串JSON_WATER_TEMP=“waterTemp_C”;
最终字符串JSON_SWELL_DIRECTION=“swellDir16Point”;
最终字符串JSON\u SWELL\u HEIGHT=“swellHeight\u m”;
最后一个字符串JSON_TIDE=“tideTime”;
JSONObject forecastJson=新的JSONObject(forecastJsonStr);
JSONArray weatherArray=forecastJson.getJSONObject(JSON_数据).getJSONArray(JSON_天气);
int numDays=weatherArray.length();
mDayForecastArray=新的DayForecast[numDays];
对于(int i=0;iprivate 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;

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