说明
- 可以快速解析Json转换为JavaBean。
- 方便Gson的使用
- 打开Android Studio的Settings设置
- 点击Plugins
- 点击Browse Repositories
- 输入GsonFormat,然后安装
- 重启Android Studio
- 新建一个java文件并且打开
- 点击菜单栏里面的Code,选择Generate(也可以直接使用快捷键alt+insert)
- 在弹出来的菜单中选择GsonFormat
- 输入Json,然后点击OK
Gson的使用
- 获取Gson对象。
Gson gson = new Gson();
- 获取JavaBean对象。
Bean bean = gson.fromJson(response.toString(), EnvironmentBean.class);
- 其中
response.toString()
是返回的Json字符串,EnvironmentBean.class
是JavaBean文件。
public class EnvironmentBean {
/**
* msg : success
* result : [{"aqi":44,"city":"广州","district":"广州","fetureData":[{"aqi":25,"date":"2016-03-10","quality":"优"},{"aqi":23,"date":"2016-03-11","quality":"优"},{"aqi":29,"date":"2016-03-12","quality":"优"},{"aqi":37,"date":"2016-03-13","quality":"优"},{"aqi":50,"date":"2016-03-14","quality":"优"},{"aqi":48,"date":"2016-03-15","quality":"优"}],"hourData":[{"aqi":76,"dateTime":"2016-03-09 10:00:00"},{"aqi":77,"dateTime":"2016-03-09 09:00:00"},{"aqi":75,"dateTime":"2016-03-09 08:00:00"},{"aqi":70,"dateTime":"2016-03-09 07:00:00"},{"aqi":66,"dateTime":"2016-03-09 06:00:00"},{"aqi":56,"dateTime":"2016-03-09 05:00:00"},{"aqi":54,"dateTime":"2016-03-09 04:00:00"},{"aqi":51,"dateTime":"2016-03-09 03:00:00"},{"aqi":45,"dateTime":"2016-03-09 02:00:00"},{"aqi":51,"dateTime":"2016-03-09 01:00:00"},{"aqi":47,"dateTime":"2016-03-09 00:00:00"},{"aqi":51,"dateTime":"2016-03-08 23:00:00"},{"aqi":52,"dateTime":"2016-03-08 22:00:00"},{"aqi":56,"dateTime":"2016-03-08 21:00:00"},{"aqi":61,"dateTime":"2016-03-08 20:00:00"},{"aqi":61,"dateTime":"2016-03-08 19:00:00"},{"aqi":60,"dateTime":"2016-03-08 18:00:00"},{"aqi":55,"dateTime":"2016-03-08 17:00:00"},{"aqi":51,"dateTime":"2016-03-08 16:00:00"},{"aqi":48,"dateTime":"2016-03-08 15:00:00"},{"aqi":48,"dateTime":"2016-03-08 14:00:00"},{"aqi":53,"dateTime":"2016-03-08 13:00:00"},{"aqi":54,"dateTime":"2016-03-08 12:00:00"},{"aqi":62,"dateTime":"2016-03-08 11:00:00"}],"no2":54,"pm10":74,"pm25":56,"province":"广东","quality":"优","so2":18,"updateTime":"2016-03-09 11:00:00"}]
* retCode : 200
*/
private String msg;
private String retCode;
/**
* aqi : 44
* city : 广州
* district : 广州
* fetureData : [{"aqi":25,"date":"2016-03-10","quality":"优"},{"aqi":23,"date":"2016-03-11","quality":"优"},{"aqi":29,"date":"2016-03-12","quality":"优"},{"aqi":37,"date":"2016-03-13","quality":"优"},{"aqi":50,"date":"2016-03-14","quality":"优"},{"aqi":48,"date":"2016-03-15","quality":"优"}]
* hourData : [{"aqi":76,"dateTime":"2016-03-09 10:00:00"},{"aqi":77,"dateTime":"2016-03-09 09:00:00"},{"aqi":75,"dateTime":"2016-03-09 08:00:00"},{"aqi":70,"dateTime":"2016-03-09 07:00:00"},{"aqi":66,"dateTime":"2016-03-09 06:00:00"},{"aqi":56,"dateTime":"2016-03-09 05:00:00"},{"aqi":54,"dateTime":"2016-03-09 04:00:00"},{"aqi":51,"dateTime":"2016-03-09 03:00:00"},{"aqi":45,"dateTime":"2016-03-09 02:00:00"},{"aqi":51,"dateTime":"2016-03-09 01:00:00"},{"aqi":47,"dateTime":"2016-03-09 00:00:00"},{"aqi":51,"dateTime":"2016-03-08 23:00:00"},{"aqi":52,"dateTime":"2016-03-08 22:00:00"},{"aqi":56,"dateTime":"2016-03-08 21:00:00"},{"aqi":61,"dateTime":"2016-03-08 20:00:00"},{"aqi":61,"dateTime":"2016-03-08 19:00:00"},{"aqi":60,"dateTime":"2016-03-08 18:00:00"},{"aqi":55,"dateTime":"2016-03-08 17:00:00"},{"aqi":51,"dateTime":"2016-03-08 16:00:00"},{"aqi":48,"dateTime":"2016-03-08 15:00:00"},{"aqi":48,"dateTime":"2016-03-08 14:00:00"},{"aqi":53,"dateTime":"2016-03-08 13:00:00"},{"aqi":54,"dateTime":"2016-03-08 12:00:00"},{"aqi":62,"dateTime":"2016-03-08 11:00:00"}]
* no2 : 54
* pm10 : 74
* pm25 : 56
* province : 广东
* quality : 优
* so2 : 18
* updateTime : 2016-03-09 11:00:00
*/
private List<Result> result;
@Override
public String toString() {
return "EnvironmentBean{" +
"msg='" + msg + '\'' +
", retCode='" + retCode + '\'' +
", result=" + result +
'}';
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setRetCode(String retCode) {
this.retCode = retCode;
}
public void setResult(List<Result> result) {
this.result = result;
}
public String getMsg() {
return msg;
}
public String getRetCode() {
return retCode;
}
public List<Result> getResult() {
return result;
}
public static class Result {
private int aqi;
private String city;
private String district;
private int no2;
private int pm10;
private int pm25;
private String province;
private String quality;
private int so2;
private String updateTime;
/**
* aqi : 25
* date : 2016-03-10
* quality : 优
*/
private List<FetureData> fetureData;
/**
* aqi : 76
* dateTime : 2016-03-09 10:00:00
*/
private List<HourData> hourData;
@Override
public String toString() {
return "Result{" +
"aqi=" + aqi +
", city='" + city + '\'' +
", district='" + district + '\'' +
", no2=" + no2 +
", pm10=" + pm10 +
", pm25=" + pm25 +
", province='" + province + '\'' +
", quality='" + quality + '\'' +
", so2=" + so2 +
", updateTime='" + updateTime + '\'' +
", fetureData=" + fetureData +
", hourData=" + hourData +
'}';
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public void setCity(String city) {
this.city = city;
}
public void setDistrict(String district) {
this.district = district;
}
public void setNo2(int no2) {
this.no2 = no2;
}
public void setPm10(int pm10) {
this.pm10 = pm10;
}
public void setPm25(int pm25) {
this.pm25 = pm25;
}
public void setProvince(String province) {
this.province = province;
}
public void setQuality(String quality) {
this.quality = quality;
}
public void setSo2(int so2) {
this.so2 = so2;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public void setFetureData(List<FetureData> fetureData) {
this.fetureData = fetureData;
}
public void setHourData(List<HourData> hourData) {
this.hourData = hourData;
}
public int getAqi() {
return aqi;
}
public String getCity() {
return city;
}
public String getDistrict() {
return district;
}
public int getNo2() {
return no2;
}
public int getPm10() {
return pm10;
}
public int getPm25() {
return pm25;
}
public String getProvince() {
return province;
}
public String getQuality() {
return quality;
}
public int getSo2() {
return so2;
}
public String getUpdateTime() {
return updateTime;
}
public List<FetureData> getFetureData() {
return fetureData;
}
public List<HourData> getHourData() {
return hourData;
}
public static class FetureData {
private int aqi;
private String date;
private String quality;
@Override
public String toString() {
return "FetureData{" +
"aqi=" + aqi +
", date='" + date + '\'' +
", quality='" + quality + '\'' +
'}';
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public void setDate(String date) {
this.date = date;
}
public void setQuality(String quality) {
this.quality = quality;
}
public int getAqi() {
return aqi;
}
public String getDate() {
return date;
}
public String getQuality() {
return quality;
}
}
public static class HourData {
private int aqi;
private String dateTime;
@Override
public String toString() {
return "HourData{" +
"aqi=" + aqi +
", dateTime='" + dateTime + '\'' +
'}';
}
public void setAqi(int aqi) {
this.aqi = aqi;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public int getAqi() {
return aqi;
}
public String getDateTime() {
return dateTime;
}
}
}
}