enable将json数组解析为数组

时间:2015-11-13 16:51:09

标签: android arrays json parsing

当我只解析4个实体cloudcover,humidity,tempc,tempf时,它成功运行但是当我解析图像值时,它会从服务器解析数据时出错。这是我的json链接JSON File

这是我的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;
}

2 个答案:

答案 0 :(得分:2)

您应该从

更改代码
 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);
     }    

要,

 JSONArray jsonArray = currentData.getJSONArray("weatherIconUrl");
 for (int j=0;j< jsonArray.length();j++)
  {
    JSONObject object1 = jsonArray.getJSONObject(j);
    weather.setImage(object1.getString("value"));
   }

解释

  1. 您正试图在weatherDesc内获取图片。它实际上可以在weatherIconUrl数组中使用。

  2. 在循环weatherIconUrl数组时,您应该导航到JSONObject内部,

     JSONObject object1 = jsonArray.getJSONObject(j);
    
  3. 从循环中删除weatherList.add(weather);,因为循环后有相同的语句。没有必要。

答案 1 :(得分:1)

您可以用1行替换for循环来获取图片网址:

weather.setImage(weatherData.getJSONArray("weatherIconUrl").getJSONObject(0)
                   .getString("value"));