浅谈JSONObject解析JSON数据

Author Avatar
Mr-houzi 6月 23, 2018
  • 在其它设备中阅读本文章

根据一段天气API来说一下JSONObject如何解析json数据,尽管现在在开发中使用Gson等,对于像我这样初次使用Java做开发的小白,说一下也是好的。

JSON数据

选取这段json数据是因为这段数据还是比较复杂的,能涵盖要说的关键点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{
"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返回的数据

1
2
3
4
5
6
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下的数据。

1
2
3
//解析数据
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");

此时,jsonData中数据为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"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进行读值即可

1
2
3
4
5
6
7
8
//解析天气
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下的数据

1
2
3
4
5
//解析数据
JSONObject jsonObject = new JSONObject(t);
JSONObject jsonData = jsonObject.getJSONObject("data");
JSONArray jsonIndex =jsonData.getJSONArray("index");
JSONArray jsonDaily =jsonData.getJSONArray("daily");

方法一

此时,jsonDaily中数据为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[
{
"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<>,datesweeksweathers等,然后进行for循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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中数据为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[
{
"name": "化妆指数",
"level": "控油",
"msg": "建议用露质面霜打底,水质无油粉底霜,透明粉饼,粉质胭脂。"
},
{
"name": "感冒指数",
"level": "易发",
"msg": "感冒容易发生,少去人群密集的场所有利于降低感冒的几率。"
},
{
"name": "洗车指数",
"level": "不宜",
"msg": "雨(雪)水和泥水会弄脏您的爱车,不适宜清洗车辆。"
},
{
"name": "穿衣指数",
"level": "舒适",
"msg": "白天温度适中,但早晚凉,易穿脱的便携外套很实用。"
},
{
"name": "紫外线强度指数",
"level": "弱",
"msg": "辐射较弱,涂擦SPF12-15、PA+护肤品。"
},
{
"name": "运动指数",
"level": "不适宜",
"msg": "受到阵雨天气的影响,不宜在户外运动。"
}
]

jsonArray为二维数组,我们通过两个嵌套循环进行遍历。首先,外层根据数组长度进行for循环遍历;然后内层使用迭代器进行遍历。

1
2
3
4
5
6
7
8
9
10
11
12
13
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