要解析的数据
[
{
"airCondition": "良",
"city": "保定",
"coldIndex": "易发期",
"date": "2018-06-21",
"distrct": "保定",
"dressingIndex": "薄短袖类",
"exerciseIndex": "不适宜",
"future": [
{
"date": "2018-06-21",
"dayTime": "多云",
"night": "阴",
"temperature": "35°C / 24°C",
"week": "今天",
"wind": "南风 3~4级"
},
{
"date": "2018-06-22",
"dayTime": "多云",
"night": "多云",
"temperature": "34°C / 24°C",
"week": "星期五",
"wind": "南风 3~4级"
},
{
"date": "2018-06-23",
"dayTime": "晴",
"night": "多云",
"temperature": "36°C / 25°C",
"week": "星期六",
"wind": "南风 小于3级"
},
{
"date": "2018-06-24",
"dayTime": "多云",
"night": "多云",
"temperature": "35°C / 23°C",
"week": "星期日",
"wind": "南风 3~4级"
},
{
"date": "2018-06-25",
"dayTime": "阴",
"night": "阴",
"temperature": "35°C / 25°C",
"week": "星期一",
"wind": "南风 3~4级"
},
{
"date": "2018-06-26",
"dayTime": "多云",
"night": "多云",
"temperature": "34°C / 24°C",
"week": "星期二",
"wind": "南风 小于3级"
}
],
"humidity": "湿度:55%",
"pollutionIndex": "78",
"province": "河北",
"sunrise": "04:59",
"sunset": "19:45",
"temperature": "29℃",
"time": "09:21",
"updateTime": "20180621093612",
"washIndex": "非常适宜",
"weather": "阴",
"week": "周四",
"wind": "西风1级"
}
]
1.定义一个bean
实现方式:新建一个java类,右键
“Generate...”
,选择“GsonFormat”
,将json数据复制到输入框,点击“OK”
,即可生成一个bean,如下所示:
public class WeatherBean {
private String airCondition;
private String city;
private String coldIndex;
private String date;
private String distrct;
private String dressingIndex;
private String exerciseIndex;
private String humidity;
private String pollutionIndex;
private String province;
private String sunrise;
private String sunset;
private String temperature;
private String time;
private String updateTime;
private String washIndex;
private String weather;
private String week;
private String wind;
private List future;
public String getAirCondition() {
return airCondition;
}
public void setAirCondition(String airCondition) {
this.airCondition = airCondition;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getColdIndex() {
return coldIndex;
}
public void setColdIndex(String coldIndex) {
this.coldIndex = coldIndex;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDistrct() {
return distrct;
}
public void setDistrct(String distrct) {
this.distrct = distrct;
}
public String getDressingIndex() {
return dressingIndex;
}
public void setDressingIndex(String dressingIndex) {
this.dressingIndex = dressingIndex;
}
public String getExerciseIndex() {
return exerciseIndex;
}
public void setExerciseIndex(String exerciseIndex) {
this.exerciseIndex = exerciseIndex;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getPollutionIndex() {
return pollutionIndex;
}
public void setPollutionIndex(String pollutionIndex) {
this.pollutionIndex = pollutionIndex;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getSunrise() {
return sunrise;
}
public void setSunrise(String sunrise) {
this.sunrise = sunrise;
}
public String getSunset() {
return sunset;
}
public void setSunset(String sunset) {
this.sunset = sunset;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getUpdateTime() {
return updateTime;
}
public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
}
public String getWashIndex() {
return washIndex;
}
public void setWashIndex(String washIndex) {
this.washIndex = washIndex;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public List getFuture() {
return future;
}
public void setFuture(List future) {
this.future = future;
}
public static class FutureBean {
private String date;
private String dayTime;
private String night;
private String temperature;
private String week;
private String wind;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDayTime() {
return dayTime;
}
public void setDayTime(String dayTime) {
this.dayTime = dayTime;
}
public String getNight() {
return night;
}
public void setNight(String night) {
this.night = night;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
}
}
注意
- 内部嵌套的类必须是static的,要不然解析会出错;
- 类里面的属性名必须跟Json字段里面的Key是一模一样的;
- 内部嵌套的用[]括起来的部分是一个List,所以定义为 public List b
,而只用{}嵌套的就定义为public C c
。
2.Gson解析json字符串
Gson gson = new Gson();
String jsonStr = gson.toJson(map.get("result")); //获取返回结果字符串
Type type = new TypeToken>() {
}.getType(); //List类型
List weathers = gson.fromJson(jsonStr, type); //天气数组
3.获取数据
List future = weatherBean.getFuture(); //FutureBean数据
weatherBean.getCity() //weatherBean其他数据