前言
在现如今的Android开发中,尤其是互联网软件,客户端与服务器端的交互可谓是家常便饭,而在Android端,通过访问接口接收到服务器端返回的Json格式的数据的情形几乎百分之九十的开发者都会遇到,这篇文章就对一些基本的到复杂的Json数据的解析进行一个全面的分析,从实战出发,至少希望你看完,能知道怎么做。
一、Json和Gson
Json是当前业内使用最为广泛的一种数据传输格式,大多数服务器端的API使用JSON作为数据的返回格式,也就是大家知道的,采用键值对的方式来记录数据。
Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。
其实一句话来说,json是一种数据格式,便于数据传输、存储、交换,而gson是一种组件库,可以把java对象数据转换成json数据格式。
二、常规的Json数据解析
使用Gson对Json数据进行解析,其实只要根据Json数据设计好你的实体类,就没问题了,从实战出发,看Json:
{ "resultcode":"200",
"reason":"successed!",
"result":{
"base":{
"temp":"24",
"wind_direction":"东北风",
"wind_strength":"2级",
"humidity":"28%",
"time":"17:38"
},
"today":{
"temperature":"15℃~26℃",
"weather":"多云转晴",
"wind":"东北风微风",
"week":"星期日",
},
},
"error_code":0
}
Json数据的层次都很清晰,键值对的映射也一目了然,上面是一个查询天气的接口返回的数据,从外到内,你可以理解为有resultcode,reason,result,error_code四个类对象,而reslut里面还包含base和today两个类对象,以此类推。那么接下来就根据这个数据格式在你的工程中创建对应的实体类,当然你可以使用Android Studio的GsonFormat插件偷一偷懒:
安装此插件后,新建一个实体类,如新建一个WeatherEntity类,然后在类文件中调用菜单使用该插件:
然后把你需要解析的Json数据复制粘贴到弹窗中,点OK就可以了,是不是很傻瓜式呢:
之后你的实体类就创建好了:
public class WeatherEntity {
/**
* resultcode : 200
* reason : successed!
* result : {"base":{"temp":"24","wind_direction":"东北风","wind_strength":"2级","humidity":"28%","time":"17:38"},"today":{"temperature":"15℃~26℃","weather":"多云转晴","wind":"东北风微风","week":"星期日"}}
* error_code : 0
*/
private String resultcode;
private String reason;
private ResultBean result;
private int error_code;
public String getResultcode() {
return resultcode;
}
public void setResultcode(String resultcode) {
this.resultcode = resultcode;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
/**
* base: {"temp":"24","wind_direction":"东北风","wind_strength":"2级","humidity":"28%","time":"17:38"}
* today : {"temperature":"15℃~26℃","weather":"多云转晴","wind":"东北风微风","week":"星期日"}
*/
private BaseBean base;
private TodayBean today;
public BaseBean getBase() {
return base;
}
public void setBase(BaseBean base) {
this.base= base;
}
public TodayBean getToday() {
return today;
}
public void setToday(TodayBean today) {
this.today = today;
}
public static class BaseBean {
/**
* temp : 24
* wind_direction : 东北风
* wind_strength : 2级
* humidity : 28%
* time : 17:38
*/
private String temp;
private String wind_direction;
private String wind_strength;
private String humidity;
private String time;
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWind_direction() {
return wind_direction;
}
public void setWind_direction(String wind_direction) {
this.wind_direction = wind_direction;
}
public String getWind_strength() {
return wind_strength;
}
public void setWind_strength(String wind_strength) {
this.wind_strength = wind_strength;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
public static class TodayBean {
/**
* temperature : 15℃~26℃
* weather : 多云转晴
* wind : 东北风微风
* week : 星期日
*/
private String temperature;
private String weather;
private String wind;
private String week;
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
}
}
}
需要提醒各位的是,实体类的类名你可以按自己心情定,但是对象名一定要与Json数据中的key一一对应,如上面的“private ResultBean result”里的"result",就不能随意取名。
然后在网络请求中直接使用如下语句就可以将Json数据转化到你的实体类对象了:
Gson gson = new Gson();
WeatherEntity weatherEntity = gson.fromJson(result, WeatherEntity .class);//result就是服务器返回的Json字符串
三、解析key为动态未知字段的Json数据
上面是一个很简单很标准的Json数据,每一个key指向一个value,key不会发生变化,不同的只是其中的value,但是如果该Json数据加上以下的内容,你还会不会正常的解析出来呢:
{ "resultcode":"200",
"reason":"successed!",
"result":{
"base":{
"temp":"24",
"wind_direction":"东北风",
"wind_strength":"2级",
"humidity":"28%",
"time":"17:38"
},
"today":{
"temperature":"15℃~26℃",
"weather":"多云转晴",
"wind":"东北风微风",
"week":"星期日",
},
"future":{
"day_20181011":{"temperature":"15℃~26℃","weather":"多云转晴"},
"day_20181012":{"temperature":"16℃~27℃","weather":"晴转多云"},
"day_20181013":{"temperature":"16℃~26℃","weather":"多云转晴"},
}
},
"error_code":0
}
如上述Json数据,在天气数据中增加了未来几天的天气,如果你依然按照之前的方法,对该数据进行类实体化,那么可想而知你的Future类里会出现以下三个类:day_20181011类,day_20181011类和day_20181011类,因为Gson是高度封装的,你的key是什么,他就会根据你的key生成对应的类,用这种传统的方法无法对这种key是动态的,未知的情况进行处理,像天气这种数据,每一天的日期都不同,采用这种动态值作为key的时候,我们该如何解析呢?
答案是,在Gson中,我们可以用Map的形式来对这种动态key的Json数据进行解析,例如上面的Future类,里面的key是动态可变的日期,值是一个固定的天气类数据(温度和天气类型),那么我们可以如下表示该字段:
private Map<String,FutureWeather> future;//String就对应着动态变化的day_20181011、day_20181012...
public static class FutureWeather{
private String temperature;
private String weather;
.........//get set 方法...
}
明白了吗,当然对应的该Map对象的属性名一定要为"future",与Json数据中的字段对应,这一点一定要注意,所以上述Json完整的实体类应为(此处略去set/get方法):
public class WeatherEntity {
/**
* resultcode : 200
* reason : successed!
* result : {"base":{"temp":"24","wind_direction":"东北风","wind_strength":"2级","humidity":"28%","time":"17:38"},"today":{"temperature":"15℃~26℃","weather":"多云转晴","wind":"东北风微风","week":"星期日"}}
* error_code : 0
*/
private String resultcode;
private String reason;
private ResultBean result;
private int error_code;
public static class ResultBean {
/**
* base: {"temp":"24","wind_direction":"东北风","wind_strength":"2级","humidity":"28%","time":"17:38"}
* today : {"temperature":"15℃~26℃","weather":"多云转晴","wind":"东北风微风","week":"星期日"}
*/
private BaseBean base;
private TodayBean today;
private Map<String,FutureWeather> future;
public static class FutureWeather{
private String temperature;
private String weather;
}
public static class BaseBean {
/**
* temp : 24
* wind_direction : 东北风
* wind_strength : 2级
* humidity : 28%
* time : 17:38
*/
private String temp;
private String wind_direction;
private String wind_strength;
private String humidity;
private String time;
}
public static class TodayBean {
/**
* temperature : 15℃~26℃
* weather : 多云转晴
* wind : 东北风微风
* week : 星期日
*/
private String temperature;
private String weather;
private String wind;
private String week;
}
}
}
接下来你就可以使用同样的方法把Json数据转化为实体类了。
使用Gson解析Json数据的方法今天暂且写这么多,大家可能已经遇到了更为复杂的数据,但是万变不离其宗,也欢迎大家留言讨论~
祝大家敲的刺激,敲的愉快(滑稽)~