Когда я проанализировал только 4 объекта cloudcover, влажность, tempc, tempf, он успешно работал, но когда я разбираю значение изображения, тогда он дает ошибку для анализа данных с сервера. Это моя ссылка JSON JSON Fileвключить синтаксический анализ массива json в массиве
это мой JSON файл
{
"data": {
"current_condition": [
{
"cloudcover": "3",
"humidity": "28",
"observation_time": "04:47 PM",
"precipMM": "0.0",
"pressure": "1014",
"temp_C": "33",
"temp_F": "91",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Clear"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0008_clear_sky_night.png"
}
],
"winddir16Point": "NNW",
"winddirDegree": "343",
"windspeedKmph": "13",
"windspeedMiles": "8"
}
],
"request": [
{
"query": "Rajkot, India",
"type": "City"
}
],
"weather": [
{
"date": "2015-11-13",
"precipMM": "0.0",
"tempMaxC": "38",
"tempMaxF": "100",
"tempMinC": "27",
"tempMinF": "80",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "NNE",
"winddirDegree": "33",
"winddirection": "NNE",
"windspeedKmph": "13",
"windspeedMiles": "8"
},
{
"date": "2015-11-14",
"precipMM": "0.0",
"tempMaxC": "37",
"tempMaxF": "98",
"tempMinC": "26",
"tempMinF": "78",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "NNE",
"winddirDegree": "27",
"winddirection": "NNE",
"windspeedKmph": "13",
"windspeedMiles": "8"
},
{
"date": "2015-11-15",
"precipMM": "0.0",
"tempMaxC": "35",
"tempMaxF": "95",
"tempMinC": "25",
"tempMinF": "76",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "N",
"winddirDegree": "5",
"winddirection": "N",
"windspeedKmph": "14",
"windspeedMiles": "9"
},
{
"date": "2015-11-16",
"precipMM": "0.0",
"tempMaxC": "35",
"tempMaxF": "96",
"tempMinC": "25",
"tempMinF": "77",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "NNE",
"winddirDegree": "13",
"winddirection": "NNE",
"windspeedKmph": "16",
"windspeedMiles": "10"
},
{
"date": "2015-11-17",
"precipMM": "0.0",
"tempMaxC": "36",
"tempMaxF": "96",
"tempMinC": "25",
"tempMinF": "76",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "N",
"winddirDegree": "5",
"winddirection": "N",
"windspeedKmph": "17",
"windspeedMiles": "10"
}
]
}
}
Это мой код для разбора JSON. он работает отлично, когда я разбираю только 4 элемента. Но когда я пытаюсь получить доступ к изображению «значение» это даст ошибку позволяют получить данные с сервера
JSONObject jsono = new JSONObject(data);
JSONObject weatherData = jsono.getJSONObject("data");
JSONArray currentData= weatherData.getJSONArray("current_condition");
for (int i = 0; i < currentData.length(); i++) {
JSONObject object = currentData.getJSONObject(i);
Weather weather = new Weather();
weather.setCloudcover(object.getString("cloudcover"));
weather.setHumatity(object.getString("humidity"));
weather.setTempc(object.getString("temp_C"));
weather.setTempf(object.getString("temp_F"));
JSONArray jsonArray = weatherData.getJSONArray("weatherDesc");
for (int j=0;j< jsonArray.length();j++)
{
JSONObject object1 = new JSONObject("weatheDesc");
weather.setImage(object1.getString("value"));
weatherList.add(weather);
}
weatherList.add(weather);
}
return true;
}
//------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
Вы должны взглянуть на библиотеку Gson, она заполняет вашу модель данными json, что упрощает ее синтаксический анализ. – Nanoc
но я не знаю, как использовать gson – viratpuar
'weatherData.getJSONArray (" weatherDesc ")' не работает, потому что там нет массива. Это должно быть 'currentData.getJSONArray (" weatherDesc ")' –