Android中解析JSON数组数组

时间:2015-11-13 10:38:43

标签: android arrays json

我想从WorldWeatherOnline解析简单的天气数据 但是当我发出请求并用JSON解析它时,我的列表中没有响应 这是来自WWO的JSON数据:

{
    "data": {
        "current_condition": [
            {
                "cloudcover": "0",
                "humidity": "17",
                "observation_time": "10:47 AM",
                "precipMM": "0.0",
                "pressure": "1013",
                "temp_C": "37",
                "temp_F": "98",
                "visibility": "10",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "NNE",
                "winddirDegree": "16",
                "windspeedKmph": "10",
                "windspeedMiles": "6"
            }
        ],
        "request": [
            {
                "query": "Rajkot, India",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2015-11-13",
                "precipMM": "0.0",
                "tempMaxC": "37",
                "tempMaxF": "98",
                "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": "33",
                "winddirection": "NNE",
                "windspeedKmph": "14",
                "windspeedMiles": "9"
            },
            {
                "date": "2015-11-14",
                "precipMM": "0.0",
                "tempMaxC": "36",
                "tempMaxF": "97",
                "tempMinC": "25",
                "tempMinF": "76",
                "weatherCode": "116",
                "weatherDesc": [
                    {
                        "value": "Partly Cloudy"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
                    }
                ],
                "winddir16Point": "NNE",
                "winddirDegree": "17",
                "winddirection": "NNE",
                "windspeedKmph": "14",
                "windspeedMiles": "9"
            },
            {
                "date": "2015-11-15",
                "precipMM": "0.0",
                "tempMaxC": "36",
                "tempMaxF": "96",
                "tempMinC": "24",
                "tempMinF": "76",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "N",
                "winddirDegree": "355",
                "winddirection": "N",
                "windspeedKmph": "15",
                "windspeedMiles": "9"
            },
            {
                "date": "2015-11-16",
                "precipMM": "0.0",
                "tempMaxC": "36",
                "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": "15",
                "winddirection": "NNE",
                "windspeedKmph": "17",
                "windspeedMiles": "11"
            },
            {
                "date": "2015-11-17",
                "precipMM": "0.0",
                "tempMaxC": "37",
                "tempMaxF": "98",
                "tempMinC": "25",
                "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": "15",
                "windspeedMiles": "9"
            }
        ]
    }
}

这是我解析JSON的代码

if (status == 200) {
    HttpEntity entity = response.getEntity();
    String data = EntityUtils.toString(entity);

    JSONObject jsono = new JSONObject(data);
    JSONArray jarray = jsono.getJSONArray("current_condition");

    for (int i = 0; i < jarray.length(); i++) {
        JSONObject object = jarray.getJSONObject(i);

        Weather weather = new Weather();
        weather.cloudcover(object.getString("cloudcover"));
        weather.sethumidity(object.getString("humidity"));
        actor.setweatherDesc(object.getString("weatherDesc"));
        weatherList.add(weather);
    }
}

请有人提出如何解析这4个参数的想法。提前致谢

1 个答案:

答案 0 :(得分:0)

当前条件位于数据json对象内部,您不会进入该对象。试试这个

                    JSONObject jsono = new JSONObject(data);
                    JSONObject weatherData = jsono.getJSONObject("data"); 
                    JSONArray currentData = weatherData.getJSONArray("current_condition");