最近在做一个天气应用的时候,想解析一个超级复杂的json数据如下
{
"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body": { "cityInfo": { "c1": "101010100", "c10": "1", "c11": "010", "c12": "100000", "c15": "33", "c16": "AZ9010", "c17": "+8", "c2": "beijing", "c3": "北京", "c4": "beijing", "c5": "北京", "c6": "beijing", "c7": "北京", "c8": "china", "c9": "中国", "latitude": 39.904, "longitude": 116.391 }, "f1": { "air_press": "1015 hPa", "day": "20160426", "day_air_temperature": "24", "day_weather": "多云", "day_weather_code": "01", "day_weather_pic": "http://app1.showapi.com/weather/icon/day/01.png", "day_wind_direction": "无持续风向", "day_wind_power": "微风<10m/h", "jiangshui": "9%", "night_air_temperature": "14", "night_weather": "阴", "night_weather_code": "02", "night_weather_pic": "http://app1.showapi.com/weather/icon/night/02.png", "night_wind_direction": "无持续风向", "night_wind_power": "微风<10m/h", "sun_begin_end": "05:20|19:03", "weekday": 2, "ziwaixian": "最弱" }, "f2": { "day": "20160427", "day_air_temperature": "18", "day_weather": "阴", "day_weather_code": "02", "day_weather_pic": "http://app1.showapi.com/weather/icon/day/02.png", "day_wind_direction": "无持续风向", "day_wind_power": "微风<10m/h", "night_air_temperature": "12", "night_weather": "多云", "night_weather_code": "01", "night_weather_pic": "http://app1.showapi.com/weather/icon/night/01.png", "night_wind_direction": "无持续风向", "night_wind_power": "微风<10m/h", "sun_begin_end": "05:20|19:03", "weekday": 3 }, "f3": { "day": "20160428", "day_air_temperature": "25", "day_weather": "晴", "day_weather_code": "00", "day_weather_pic": "http://app1.showapi.com/weather/icon/day/00.png", "day_wind_direction": "无持续风向", "day_wind_power": "微风<10m/h", "night_air_temperature": "13", "night_weather": "晴", "night_weather_code": "00", "night_weather_pic": "http://app1.showapi.com/weather/icon/night/00.png", "night_wind_direction": "无持续风向", "night_wind_power": "微风<10m/h", "sun_begin_end": "05:20|19:03", "weekday": 4 }, "now": { "aqi": 122, "aqiDetail": { "aqi": 122, "area": "北京", "area_code": "beijing", "co": 1.255, "no2": 37, "o3": 138, "o3_8h": 86, "pm10": 139, "pm2_5": 92, "primary_pollutant": "颗粒物(PM2.5)", "quality": "轻度污染", "so2": 29 }, "sd": "31%", "temperature": "22", "temperature_time": "16:30", "weather": "多云", "weather_code": "01", "weather_pic": "http://appimg.showapi.com/images/weather/icon/day/01.png", "wind_direction": "东南风", "wind_power": "2级" }, "ret_code": 0, "time": "20160426113000" } }
看到这json,瞬间晕了,现在一般都会采用Google推出的gson或者是阿里巴巴推出的fastjson这两种方式去解析json数据。
查了文档,发现gson其实就是把Json数据转化成一个Bean对象去存储。如上面的json数据对应一个weather对象。如何生成我们的weather对象呢,在android studio中存在一个GsonFormat的工具,你可以点击setting->plugins->输入工具名称下载,安装好gsonformat工具重启as就可以使用了。
那怎么使用嘞!四步走的方式吧
1.新建一个weather类并且选中该类
package top.rush_yu.myapplication;
/** * 作者:Created by rush_yu on 2016/4/26 19:36 * 邮箱:[email protected] * version 1.0 */
public class weather {
}
2.点击setting并且选中gsonFormat工具单击Ok即可
3.选中weather类,按快捷键alt+insert或者点Code->generate可见如下对话框
点击GsonFormat界面并在里面设置你需装换成weather类的json数据
4.输入完成后点击两个ok即可。
执行完上诉步骤之后再去看一下你的weather类,
见后面源码。会发现自动生成了好多对象,以及变量,这便实现了将json数据转化成一个对象的bean类。那么我们怎么使用这个对象嘞。
只需
Gson gson = new Gson();
weather weather1 = gson.fromJson(json,weather.class);
只需上面两行代码,json的数据值就被映射成一个weather对象嘞,这时候只需直接引用即可。
废话不多说,源码直接上
http://download.csdn.net/detail/qq_29282475/9503466