JSONException:指数5超出范围[0..5)
使用此JSON对象:http://api.worldweatheronline.com/premium/v1/marine.ashx?q=-34.8799074,174.7565664&key=3477e975c4d9a8a0eaac2b2c818f4&format=json&tide=yesJSONException:指数5超出范围[0..5)
有些日子里,它工作得很好,取来自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;
}
I'm很肯定你有类似的问题: 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);
}
好的,我发现这个问题:API没有提供最后一天的所有“每小时”对象(无论出于何种原因),所以“每小时”对象的数量没有达到我的代码中定义的“afternoonObject”。因此,我简单地将预测天数限制为6天而不是7天(weatherArray.lenght())。有没有另一种方法来解决这个问题,并获得7天? – Makoto 2014-12-03 23:48:34
请发布您的代码! – 2014-12-03 22:19:23
我已添加代码。谢谢!! – Makoto 2014-12-03 23:29:32