浅谈JSONObject解析JSON数据
根据一段天气API来说一下JSONObject如何解析json数据,尽管现在在开发中使用Gson等,对于像我这样初次使用Java做开发的小白,说一下也是好的。
JSON数据
选取这段json数据是因为这段数据还是比较复杂的,能涵盖要说的关键点
{
"data": {
"city": "深圳",
"temphigh": "25",
"templow": "19",
"updatetime": "2017-11-04 13:23:00",
"tempnow": "24",
"sendibletemp": "27",
"winddirect": "东北风",
"windpower": "2级",
"humidity": "42",
"sunrise": "06:29",
"sunset": "17:45",
"weather": "多云",
"week": "星期六",
"nl": null,
"date": "2017-11-04",
"index": [
{
"name": "化妆指数",
"level": "控油",
"msg": "建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。"
},
{
"name": "感冒指数",
"level": "易发",
"msg": "感冒容易发生,少去人群密集的场所有利于降低感冒的几率。"
},
{
"name": "洗车指数",
"level": "不宜",
"msg": "雨(雪)水和泥水会弄脏您的爱车,不适宜清洗车辆。"
},
{
"name": "穿衣指数",
"level": "舒适",
"msg": "白天温度适中,但早晚凉,易穿脱的便携外套很实用。"
},
{
"name": "紫外线强度指数",
"level": "弱",
"msg": "辐射较弱,涂擦SPF12-15、PA+护肤品。"
},
{
"name": "运动指数",
"level": "不适宜",
"msg": "受到阵雨天气的影响,不宜在户外运动。"
}
],
"pm25": {
"aqi": 0,
"co": 8,
"o3": 42,
"pm10": 63,
"pm2_5": 64,
"quality": "良",
"so2": 4,
"no2": 11,
"updatetime": "2017-11-04 13:00:00"
},
"daily": [
{
"date": "2017-11-04",
"week": "星期六",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "25",
"templow": "19",
"weather": "多云"
},
{
"date": "2017-11-05",
"week": "星期日",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "26",
"templow": "19",
"weather": "多云"
},
{
"date": "2017-11-06",
"week": "星期一",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "27",
"templow": "20",
"weather": "多云"
},
{
"date": "2017-11-07",
"week": "星期二",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "21",
"weather": "多云"
},
{
"date": "2017-11-08",
"week": "星期三",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "29",
"templow": "22",
"weather": "多云"
},
{
"date": "2017-11-09",
"week": "星期四",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "22",
"weather": "多云"
},
{
"date": "2017-11-03",
"week": "星期五",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "18",
"weather": "晴"
}
]
},
"status": 0,
"msg": "ok"
}
解析JSON
利用JSONString进行简单解析
我利用了RxVolley进行数据通信,t为API返回的数据
RxVolley.get("https://chkj02.market.alicloudapi.com/qgtq?city="+city, params, new HttpCallback() {
@Override
public void onSuccess(String t) {
Loger.debug("请求到的数据:" + t);
}
});
我们现在要获取这部分数据,该如何进行解析呢?
首先,将t中的数据传到JSONObject
类型的jsonObject中,再通过getJSONObject
获取到data
下的数据。
//解析数据
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");
此时,jsonData中数据为
{
"city": "深圳",
"temphigh": "25",
"templow": "19",
"updatetime": "2017-11-04 13:23:00",
"tempnow": "24",
"sendibletemp": "27",
"winddirect": "东北风",
"windpower": "2级",
"humidity": "42",
"sunrise": "06:29",
"sunset": "17:45",
"weather": "多云",
"week": "星期六",
"nl": null,
"date": "2017-11-04"
}
然后通过getString
进行读值即可
//解析天气
String jsonTemplow = jsonData.getString("templow");
String jsonTempHigh = jsonData.getString("temphigh");
String jsonWeather = jsonData.getString("weather");
String jsonTempnow = jsonData.getString("tempnow");
String jsonWinddirect = jsonData.getString("winddirect");
String jsonWindpower = jsonData.getString("windpower");
String jsonHumidity = jsonData.getString("humidity");
利用JSONArray进行复杂解析
这次,我们要获取这部分数据
首先,将t中的数据传到JSONObject类型的jsonObject中,再通过getJSONObject
获取到data
下的数据。然后jsonArray通过getJSONArray
获得index
下的数据
//解析数据
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");
JSONArray jsonIndex =jsonData.getJSONArray("index");
JSONArray jsonDaily =jsonData.getJSONArray("daily");
方法一
此时,jsonDaily中数据为
[
{
"date": "2017-11-04",
"week": "星期六",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "25",
"templow": "19",
"weather": "多云"
},
{
"date": "2017-11-05",
"week": "星期日",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "26",
"templow": "19",
"weather": "多云"
},
{
"date": "2017-11-06",
"week": "星期一",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "27",
"templow": "20",
"weather": "多云"
},
{
"date": "2017-11-07",
"week": "星期二",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "21",
"weather": "多云"
},
{
"date": "2017-11-08",
"week": "星期三",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "29",
"templow": "22",
"weather": "多云"
},
{
"date": "2017-11-09",
"week": "星期四",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "22",
"weather": "多云"
},
{
"date": "2017-11-03",
"week": "星期五",
"sunrise": "06:29",
"sunset": "17:45",
"temphigh": "28",
"templow": "18",
"weather": "晴"
}
]
把jsonDaily中按分类进行解析,分为几个ArrayList<>,dates
、weeks
、weathers
等,然后进行for循环。
List<String> dates = new ArrayList<>();
List<String> weeks = new ArrayList<>();
List<String> weathers = new ArrayList<>();
int j=1;
for (int i=0;i<jsonArray.length();i++){
JSONObject partDaily = jsonArray.getJSONObject(i);
JSONString date = partDaily.getString("date");
dates.add(date);
JSONString week = partDaily.getString("week");
weeks.add(week);
JSONString weather = partDaily.getString("weather");
weathers.add(weather);
}
方法二
此时,jsonIndex中数据为
[
{
"name": "化妆指数",
"level": "控油",
"msg": "建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。"
},
{
"name": "感冒指数",
"level": "易发",
"msg": "感冒容易发生,少去人群密集的场所有利于降低感冒的几率。"
},
{
"name": "洗车指数",
"level": "不宜",
"msg": "雨(雪)水和泥水会弄脏您的爱车,不适宜清洗车辆。"
},
{
"name": "穿衣指数",
"level": "舒适",
"msg": "白天温度适中,但早晚凉,易穿脱的便携外套很实用。"
},
{
"name": "紫外线强度指数",
"level": "弱",
"msg": "辐射较弱,涂擦SPF12-15、PA+护肤品。"
},
{
"name": "运动指数",
"level": "不适宜",
"msg": "受到阵雨天气的影响,不宜在户外运动。"
}
]
jsonArray为二维数组,我们通过两个嵌套循环进行遍历。首先,外层根据数组长度进行for循环遍历;然后内层使用迭代器进行遍历。
String[] jsonIndex = new String[20];//数组长度声明为20确保够用
int j=1;
for (int i=0;i<jsonArray.length();i++){
JSONObject partIndex = jsonArray.getJSONObject(i);
Iterator iterator = partIndex.keys();
String key;
while(iterator.hasNext()){
//hasNext方法,只是判断下一个元素的有无,并不移动指针
key = (String) iterator.next();//next方法,向下移动指针,并且返回指针指向的元素,如果指针指向的内存中没有元素,会报异常
jsonIndex[j] = partIndex.getString(key);
j++;
}
}
这样此指数数据就被我们成功解析,然后存入jsonIndex数组中。
segmentfault文章地址 https://segmentfault.com/a/1190000015363286