之前写过一个,但是写不太全,结果自己都看不懂,希望这个以后能看懂吧。
以两个例子说明json数据解析,一个是最简单的,一个是比较难的。
必须知道的:JSONObject和JSONArray
JSONObject是在{ }内的。如{"status":"success","country":"China"}
JSONArray是在[ ]内的。如["city":"深圳","lastUpdateTime":"2022-10-13 10:05:08"]
例一(简单数据):
原数据,这是根据当前IP获取地址,经纬度等信息的json:
{"status":"success","country":"China","countryCode":"CN","region":"GD","regionName":"Guangdong","city":"Shenzhen","zip":"","lat":22.5579,"lon":114.065,"timezone":"Asia/Shanghai","isp":"ShenZhen Topway Video Communication Co. Ltd","org":"Topway","as":"AS17962 ShenZhen Topway Video Communication Co. Ltd","query":"115.45.170.249"}
美化后数据:
如要提取:status、lat、lon三个数据(分别是:获取状态,经度,纬度)
外面是{}所以是:
JSONObject jsonObjectALL = new JSONObject(json);//json是获取的源数据,详细看volley部分。
然后直接获取里面数据,直接用getstring就行了:
String status=jsonObjectALL.getString("status");
lat = jsonObjectALL.getString("lat");
lon =jsonObjectALL.getString("lon");
"status"、"lat"、"lon"引号里的值必须和原数据内的一样,注意大小写,最好复制粘贴。
例二(数据比较复杂):
原数据,这是墨迹天气获取的3天天气数据。
{"code":0,"msg":"操作成功","data":{"total":7,"sourceName":"墨迹天气","list":[{"city":"深圳","lastUpdateTime":"2022-10-13 10:05:08","date":"2022-10-13","weather":"晴","temp":23.0,"humidity":"43%","wind":"东风4级","pm25":27.0,"pm10":43.0,"low":23.0,"high":30.0,"airData":"43","airQuality":"优","dateLong":1665590400000,"weatherType":0,"windLevel":4,"province":"广东","moreData":{"sunrise":"2022-10-13 06:19:00","sunset":"2022-10-13 18:01:00","precipitation":"0","alert":[{"update_time":"2022-10-13 09:07:27","infoid":80,"level":"红色","pub_time":"2022-10-13 09:03:00","name":"森林火险","title":"深圳市气象台发布森林火险红色预警[I级/特别严重]","type":"森林火险红色","content":"【深圳市森林火险橙色预警升级为红色】深圳市森林防灭火指挥部和深圳市气象台2022年10月13日09时00分将全市陆地森林火险橙色预警升级为红色,预计我市天气将持续干燥,森林火险气象等级为五级,林内可燃物极易燃烧,森林火灾极易发生,火势蔓延速度极快,有关主管部门加大巡山护林力度,严格管制野外火源。(预警信息来源:国家预警信息发布中心)"}]}},{"city":"深圳","lastUpdateTime":"2022-10-13 09:00:00","date":"2022-10-14","weather":"晴","humidity":"未知","wind":"微风","pm25":0.0,"low":24.0,"high":31.0,"airData":"61","airQuality":"良","dateLong":1665676800000,"weatherType":0,"windLevel":2,"province":"广东","moreData":{"sunrise":"2022-10-14 06:20:00","sunset":"2022-10-14 18:00:00","precipitation":null,"alert":null}},{"city":"深圳","lastUpdateTime":"2022-10-13 09:00:00","date":"2022-10-15","weather":"多云","humidity":"未知","wind":"微风","pm25":0.0,"low":24.0,"high":31.0,"airData":"66","airQuality":"良","dateLong":1665763200000,"weatherType":1,"windLevel":1,"province":"广东","moreData":{"sunrise":"2022-10-15 06:20:00","sunset":"2022-10-15 17:59:00","precipitation":null,"alert":null}}],"logoUrl":"http://iflycar.hfdn.openstorage.cn/xfypicture/dev/logo/moji.png"}}
数据美化:
目的是要把10月13号的相关信息提取出来,通过源数据可以看出,最外面是JSONObject,里面一层data也是JSONObject,再里面的list是是JSONArray,再里面的“0”还是JSONObject, 这里才是我们要的数据。这里面的0不是真有这个值,只是表示数组的第0个数据,再后面是1、2、3。
所以要一层层解析:
JSONObject jsonObjectALL = new JSONObject(json);//最外面一层 JSONObject jsonObject1=jsonObjectALL.getJSONObject("data");//data层 JSONArray jsonArray=jsonObject1.getJSONArray("list");//list数组 JSONObject jsonObject2 = jsonArray.getJSONObject(0);//数据的第0个数据其实是个JSONObject String city = jsonObject2.getString("city");//具体数据 String weather=jsonObject2.getString("weather");
这样就可以解析出城市和天气了。
注意:
1、需要哪层数据,前面的所有“层”都要写出来。
2、每一层名字都不能相同(上面红字部分),类型也不能错,里层的数据总是指向外层的某的数据。比如:
JSONObject jsonObjectALL = new JSONObject(json);//最外面一层
JSONObject jsonObject1=jsonObjectALL.getJSONObject("data");//data层
表示的是jsonObjest1是jsonObjectALL里面的“data”.