java遍历复杂json字符串获取想要的数据
java如何解析复杂的json数据
关于json处理的包有好⼏个,⽐如jackson、Gson、Fastjson。Gson是⾕歌做的,功能强⼤;Fastjson是阿⾥巴巴做的,性能更快。具体⽤哪个,开⼼就好。我这⾥两个都没⽤,⽤的是java的⼀个类库—json-lib。如果你⽤的是maven构建的项⽬,添加依赖请注意加上JDK版本15,详情请戳这⾥
ok,多的不说了,让我们直接来解析⼀个复杂的json数据吧,如下:
String weather = {
"HeWeather6": [
{
"basic": {
"cid": "CN101010100",
"location": "北京",
"parent_city": "北京",
"admin_area": "北京",
"cnty": "中国",
"lat": "39.90498734",
"lon": "116.40528870",
"tz": "8.0"
},
"daily_forecast": [
{
"cond_code_d": "103",
"cond_code_n": "101",
"cond_txt_d": "晴间多云",
"cond_txt_n": "多云",
"date": "2017-10-26",
"hum": "57",
"pcpn": "0.0",
"pop": "0",
fastjson字符串转数组"pres": "1020",
"tmp_max": "16",
"tmp_min": "8",
"uv_index": "3",
"vis": "16",
"wind_deg": "0",
"wind_dir": "⽆持续风向",
"wind_sc": "微风",
"wind_spd": "5"
},
{
"cond_code_d": "101",
"cond_code_n": "501",
"cond_txt_d": "多云",
"cond_txt_n": "雾",
"date": "2017-10-27",
"hum": "56",
"pcpn": "0.0",
"pop": "0",
"pres": "1018",
"tmp_max": "18",
"tmp_min": "9",
"uv_index": "3",
"vis": "20",
"wind_deg": "187",
"wind_dir": "南风",
"wind_sc": "微风",
"wind_spd": "6"
},
{
"cond_code_d": "101",
"cond_code_n": "101",
"cond_txt_d": "多云",
"cond_txt_n": "多云",
"date": "2017-10-28",
"hum": "26",
"pcpn": "0.0",
"pop": "0",
"pres": "1029",
"tmp_max": "17",
"tmp_min": "5",
"uv_index": "2",
"vis": "20",
"wind_deg": "2",
"wind_dir": "北风",
"wind_sc": "3-4",
"wind_spd": "19"
}
],
"status": "ok",
"update": {
"loc": "2017-10-26 23:09",
"utc": "2017-10-26 15:09"
}
}
]
}
这⾥请先将json数据转换成json字符串,直接.toString()即可
解析之前,先简单分析⼀下该json的结构:
⾸先,最外层是⼀个{},⾥⾯只有⼀个key是"HeWeather6",对应的value是[],显然是⼀个数组;
然后,第⼆层也就是第⼀层的value。该层由⼀个{}组成,{}⾥⾯有4对key-value,分别是:
"basic": {...}
"daily_forecast" : [...]
"status":"ok"
"upate": {...}
第3对数据就不⽤多说了,第1和第4对数据也很清晰了,key对应的value都是简单的json数据;
所以,唯⼀需要再往⾥分析的就是第2对数据了,其对应的value是[],显然⼜是⼀个数组。数组⾥⾯也很明了了,也都是简单的json数据。ps:我这⾥简单的json数据,指的是key-value都是简单的字符串,没有其他复杂的数据结构。
到此,分析完毕!哇哦,是不是发现解析复杂的json数据就像剥洋葱⼀样,⼀层⼀层的往⾥剥,其实也很简单!只要学会了分析,有点耐⼼,就算再复杂的json数据也能把它给解析出来~
最后,上代码,如下(解释我全都会写在注释⾥⾯,就不再解释什么啦):
JSONObject jsonObject = JSONObject.fromObject(weather);
//获取第⼆层的数组,将其转换成JSONArray,对应的key为HeWeather6
JSONArray jsonArray = JSONArray("HeWeather6");
//获取JSONArray ⾥⾯的数据,因为jsonArray⾥⾯只有⼀个数据(⼀个{}),所以获取value值的⾓标为0
JSONObject jsonObject2 = JSONObject(0);
//获取4对数据的value,这⾥先获取最简单的第3对数据的值打印"three"可得 -> ok
String three= ("status").toString();
//然后获取第1对数据的值,先获取对应的json 然后获取json⾥⾯的value值
JSONObject one = JSONObject("basic");
String oneCid = ("cid").toString();
//打印oneCid可得 -> CN101010100 其他以此类推,第4对数据同理,就不再赘述了
//最后获取第2对数据的value值思路:先获取数组,然后遍历,分别拿到⾥⾯的值
JSONArray twos = JSONArray("daily_forecast");
JSONObject two = null;
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int i = 0; i < twos.size(); i++) {
two = JSONObject(i);
Map<String, String> map = new HashMap<String, String>();
map.put("天⽓情况", ("cond_txt_d").toString());
map.put("最⾼温度", ("tmp_max").toString());
map.put("风⼒情况", ("wind_sc").toString());
list.add(map);
}
System.out.String());完成
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论