【发布时间】: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