【问题标题】:enable to parse json array into array启用将 json 数组解析为数组
【发布时间】:2015-11-13 16:51:09
【问题描述】:

当我只解析 4 个实体 cloudcover、湿度、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;
}

【问题讨论】:

  • 你应该看看 Gson 库,它使用 json 数据填充你的模型,使其解析起来非常简单。
  • 但是我不知道如何使用gson
  • weatherData.getJSONArray("weatherDesc") 不起作用,因为没有数组可以到达那里。应该是currentData.getJSONArray("weatherDesc")
  • @cricket_007 那不应该是currentData.getJSONArray("weatherIconUrl");,因为图像在weatherIconUrl 数组中吗?
  • @YuvaRaj - 如果 OP 想要图像,那么可以。但我认为我的评论是最初错误的原因。

标签: android arrays json parsing


【解决方案1】:

你应该改变你的代码

 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);,因为循环后有相同的语句。没必要。

【讨论】:

  • weatherData 应该是currentData
  • 哦,我错过了。谢谢@cricket_007
  • 是的,必须用 JavaScript 测试才能看到
  • 另外,我认为 weatherList.add(weather) 应该从循环中删除... OP 在循环之后无论如何都有该行 - 在当前设置下,将为每个图像添加一个新的 Weather 对象,并添加两次最后一张图片。
  • 但是 JSONArray jsonArray = currentData.getJSONArray 要求 (int index) 而不是 "weatherIconUrl" 它仍然不起作用
【解决方案2】:

你可以用1行替换for循环来获取图片url:

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

【讨论】:

  • 只有一个 Object 时这很好。但是,如果 JSON 数组内部的 JSON 对象过多,则它不会获取除一开始之外的其他图像。
  • 是的,但是JSON数据中的所有weatherIconUrl数组只有1项,所以不需要循环。
  • 对于当前的数据,还可以。我只是说它可能更多,因为它在 Array 而不是 Object 内部 :)
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2011-07-02
  • 2012-05-09
  • 2011-11-02
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多