这是我不得不记录下来的一个快速解析复杂的Json数组的框架,防止在以后的工作中忘记。(复杂划重点,简单的json数据我们用JsonObject就可以完全解析出来)
示例:
{
"HeWeather6": [
{
"basic": {
"cid": "CN101280601",
"location": "shenzhen",
"parent_city": "shenzhen",
"admin_area": "guangdong",
"cnty": "China",
"lat": "22.54700089",
"lon": "114.08594513",
"tz": "+8.00"
},
"update": {
"loc": "2018-04-11 09:47",
"utc": "2018-04-11 01:47"
},
"status": "ok",
"now": {
"cloud": "75",
"cond_code": "101",
"cond_txt": "Cloudy",
"fl": "27",
"hum": "74",
"pcpn": "0.0",
"pres": "1014",
"tmp": "25",
"vis": "10",
"wind_deg": "100",
"wind_dir": "E",
"wind_sc": "1",
"wind_spd": "2"
},
"daily_forecast": [
{
"cond_code_d": "101",
"cond_code_n": "101",
"cond_txt_d": "Cloudy",
"cond_txt_n": "Cloudy",
"date": "2018-04-11",
"hum": "77",
"mr": "02:58",
"ms": "14:26",
"pcpn": "0.0",
"pop": "0",
"pres": "1013",
"sr": "06:07",
"ss": "18:42",
"tmp_max": "29",
"tmp_min": "22",
"uv_index": "11",
"vis": "20",
"wind_deg": "0",
"wind_dir": "no direction",
"wind_sc": "1-2",
"wind_spd": "1"
},
{
"cond_code_d": "101",
"cond_code_n": "101",
"cond_txt_d": "Cloudy",
"cond_txt_n": "Cloudy",
"date": "2018-04-12",
"hum": "85",
"mr": "03:39",
"ms": "15:19",
"pcpn": "0.0",
"pop": "0",
"pres": "1011",
"sr": "06:06",
"ss": "18:43",
"tmp_max": "29",
"tmp_min": "23",
"uv_index": "11",
"vis": "16",
"wind_deg": "0",
"wind_dir": "no direction",
"wind_sc": "1-2",
"wind_spd": "3"
},
{
"cond_code_d": "101",
"cond_code_n": "101",
"cond_txt_d": "Cloudy",
"cond_txt_n": "Cloudy",
"date": "2018-04-13",
"hum": "82",
"mr": "04:17",
"ms": "16:12",
"pcpn": "0.0",
"pop": "0",
"pres": "1012",
"sr": "06:06",
"ss": "18:43",
"tmp_max": "29",
"tmp_min": "23",
"uv_index": "12",
"vis": "16",
"wind_deg": "0",
"wind_dir": "no direction",
"wind_sc": "1-2",
"wind_spd": "9"
}
],
"hourly": [
{
"cloud": "38",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "21",
"hum": "68",
"pop": "1",
"pres": "1014",
"time": "2018-04-11 10:00",
"tmp": "25",
"wind_deg": "147",
"wind_dir": "SE",
"wind_sc": "1-2",
"wind_spd": "8"
},
{
"cloud": "18",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "18",
"hum": "52",
"pop": "0",
"pres": "1012",
"time": "2018-04-11 13:00",
"tmp": "27",
"wind_deg": "174",
"wind_dir": "S",
"wind_sc": "1-2",
"wind_spd": "4"
},
{
"cloud": "5",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "18",
"hum": "56",
"pop": "0",
"pres": "1010",
"time": "2018-04-11 16:00",
"tmp": "28",
"wind_deg": "178",
"wind_dir": "S",
"wind_sc": "1-2",
"wind_spd": "5"
},
{
"cloud": "2",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "21",
"hum": "79",
"pop": "0",
"pres": "1011",
"time": "2018-04-11 19:00",
"tmp": "27",
"wind_deg": "170",
"wind_dir": "S",
"wind_sc": "1-2",
"wind_spd": "5"
},
{
"cloud": "3",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "21",
"hum": "91",
"pop": "0",
"pres": "1012",
"time": "2018-04-11 22:00",
"tmp": "23",
"wind_deg": "155",
"wind_dir": "SE",
"wind_sc": "1-2",
"wind_spd": "10"
},
{
"cloud": "62",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "21",
"hum": "91",
"pop": "2",
"pres": "1011",
"time": "2018-04-12 01:00",
"tmp": "22",
"wind_deg": "140",
"wind_dir": "SE",
"wind_sc": "1-2",
"wind_spd": "10"
},
{
"cloud": "96",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "22",
"hum": "94",
"pop": "2",
"pres": "1010",
"time": "2018-04-12 04:00",
"tmp": "22",
"wind_deg": "133",
"wind_dir": "SE",
"wind_sc": "1-2",
"wind_spd": "3"
},
{
"cloud": "99",
"cond_code": "103",
"cond_txt": "Partly Cloudy",
"dew": "22",
"hum": "91",
"pop": "7",
"pres": "1011",
"time": "2018-04-12 07:00",
"tmp": "22",
"wind_deg": "140",
"wind_dir": "SE",
"wind_sc": "1-2",
"wind_spd": "1"
}
],
"lifestyle": [
{
"brf": "较舒适",
"txt": "白天天气晴好,您在这种天气条件下,会感觉早晚凉爽、舒适,午后偏热。",
"type": "comf"
},
{
"brf": "舒适",
"txt": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
"type": "drsg"
},
{
"brf": "少发",
"txt": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。",
"type": "flu"
},
{
"brf": "适宜",
"txt": "天气较好,赶快投身大自然参与户外运动,尽情感受运动的快乐吧。",
"type": "sport"
},
{
"brf": "适宜",
"txt": "天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。",
"type": "trav"
},
{
"brf": "弱",
"txt": "紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。",
"type": "uv"
},
{
"brf": "较适宜",
"txt": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。",
"type": "cw"
},
{
"brf": "中",
"txt": "气象条件对空气污染物稀释、扩散和清除无明显影响,易感人群应适当减少室外活动时间。",
"type": "air"
}
]
}
]
}
对于上面这种Object里面嵌套有很多List的Json数据,用Gson解析起来简直是不费吹灰之力,不好意思,我又强行给自己加戏了。废话不多说,面对疾风吧!!!
1、建立Bean。众所周知,解析Json最难的是建立对应的实体类,需要一一对应我们的数据才可以正确拿到json数据。
GsonFormat插件你值得拥有。(用了特大号字体来重点突出)
2、当我们请求到的Response转成String后(网络请求暂且不说,不是本次记录的重点)
Gson gson = new Gson();
final WeatherModel weatherModel = gson.fromJson(response,WeatherModel.class);
3、获取对应数据
天气状况:string_weather_icon_code = weatherModel.getHeWeather6().get(0).getNow().getCond_code();
今日天气和生活指数:
tv_weather_title.setText(weatherModel.getHeWeather6().get(0).getNow().getCond_txt()); tv_hi_temp.setText(weatherModel.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_max()+"℃"); tv_lo_temp.setText(weatherModel.getHeWeather6().get(0).getDaily_forecast().get(0).getTmp_min()+"℃"); tv_weather_hum_content.setText(weatherModel.getHeWeather6().get(0).getNow().getHum()); tv_weather_wind_content.setText(weatherModel.getHeWeather6().get(0).getNow().getWind_sc()+"级"); tv_weather_lifestyle.setText("生活指数:"+weatherModel.getHeWeather6().get(0).getLifestyle().get(1).getTxt());
真的就是So Easy!!!!!