启用解析JSON数组到阵列
当我只解析了4个实体cloudcover,humidity,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;
}
你应该从
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"));
}
说明:
你试图获取的
weatherDesc
内的图像。它实际上可用于weatherIconUrl
阵列。-
虽然循环
weatherIconUrl
阵列,您应该导航到里面JSONObject
一样,JSONObject object1 = jsonArray.getJSONObject(j);
从环取出
weatherList.add(weather);
你有循环之后相同的语句。这不是必需的。
'weatherData'应该是'currentData' –
哦,我错过了它。谢谢@ cricket_007 –
是的,必须使用JavaScript进行测试才能看到 –
获取的数据可以与1号线更换为循环得到图像的URL:
weather.setImage(weatherData.getJSONArray("weatherIconUrl").getJSONObject(0)
.getString("value"));
只有一个对象时这很好。但是,如果JSON数组内部有太多的JSON对象,那么它不会首先获取除了那个之外的其他图像。 –
是的,但JSON数据中的所有weatherIconUrl数组只有1个项目,因此不需要循环。 – David
对于当前数据,没关系。我只是说它可能更多,因为它在'Array'而不是'Object'里面:) –
你应该看看Gson库,它使用json数据填充你的模型,使它很容易解析。 – Nanoc
但我不知道如何使用gson – viratpuar
'weatherData.getJSONArray(“weatherDesc”)'不起作用,因为没有数组可以到达那里。它应该是'currentData.getJSONArray(“weatherDesc”)' –