解析json是我们必备的技能之一,解析json可以使用原生API,也可以使用第三方库。前面我们有说过使用原生API,不过在我们解析比较复杂的json数据的时候如果使用原生API会显得很麻烦。因为你要一层一层的往里解析,数据多的话可以尝试使用第三方库Gson来解析。
使用Gson解析Json数据
这次解析的是一段天气预报
{"error":0,"status":"success","date":"2017-03-02",
"results":[{"currentCity":"海口","pm25":"60",
"index":[
{"title":"穿衣","zs":"较舒适",
"tipt":"穿衣指数","des":"建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"},
{"title":"洗车","zs":"较适宜",
"tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},
{"title":"旅游","zs":"适宜",
"tipt":"旅游指数","des":"天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。"},
{"title":"感冒","zs":"较易发",
"tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},
{"title":"运动","zs":"较适宜",
"tipt":"运动指数","des":"天气较好,但风力较大,推荐您进行室内运动,若在户外运动请注意避风保暖。"},
{"title":"紫外线强度","zs":"弱",
"tipt":"紫外线强度指数","des":"紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。"}],
"weather_data":[{"date":"周四 03月02日 (实时:21℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"22 ~ 16℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东风3-4级","temperature":"23 ~ 17℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东南风3-4级","temperature":"26 ~ 19℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东南风3-4级","temperature":"28 ~ 19℃"}]}]}
我们使用Gson解析,首先需要去创建一层一层的实体类。
第一层的实体类,这一层的实体类主要是对应json数据最外层的数据(创建实体类的时候,定义的变量的名称一定要和json数据的相同)
public class One {
private String status;
private String date;
public List<Lea> results;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<Lea> getResults() {
return results;
}
public void setResults(List<Lea> results) {
this.results = results;
}
}
第二层实体类
public class Lea {
private String currentCity;
private String pm25;
private List<Wea> weather_data;
public List<Wea> getWeather_data() {
return weather_data;
}
public void setWeather_data(List<Wea> weather_data) {
this.weather_data = weather_data;
}
public String getPm25() {
return pm25;
}
public void setPm25(String pm25) {
this.pm25 = pm25;
}
public String getCurrentCity() {
return currentCity;
}
public void setCurrentCity(String currentCity) {
this.currentCity = currentCity;
}
}
第三层
创建了实体类之后我们就可以直接使用Gson来解析了,解析之前一定要确定自己导入了Gson的jar包。public class Wea { private String date; private String dayPictureUrl; private String weather; private String wind; private String temperature; public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public String getDayPictureUrl() { return dayPictureUrl; } public void setDayPictureUrl(String dayPictureUrl) { this.dayPictureUrl = dayPictureUrl; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } }
我们可以先尝试解析最外层的数据,使用gson解析数据,主要是调用已经创建好的实体类的get()方法就可以了。
Gson gson=new Gson();//创建Gson //调用gson.fromJson方法,这里面的s是你要解析的json数据,One.class是最外层的实体类 One one=gson.fromJson(s,One.class); //想调用最外层的数据的话,可以直接调用实体类的里面封装好的方法 tv2.setText(one.getResults().get(0).getCurrentCity()); tv3.setText("pm:"+one.getResults().get(0).getPm25());
我们解析更深层的数据。
如果我们想要解析数组的话,需要使用到循环。for(int i=0;i<one.getResults().get(0).getWeather_data().size();i++){ Log.w("wind",one.getResults().get(0).getWeather_data().get(i).getWind()); Log.w("data",one.getResults().get(0).getWeather_data().get(i).getDate()); Log.w("weather",one.getResults().get(0).getWeather_data().get(i).getWeather()); Log.w("temperature",one.getResults().get(0).getWeather_data().get(i).getTemperature()); a1.add(one.getResults().get(0).getWeather_data().get(i).getDate()); a1.add(one.getResults().get(0).getWeather_data().get(i).getWeather()); a1.add(one.getResults().get(0).getWeather_data().get(i).getWind()); a1.add(one.getResults().get(0).getWeather_data().get(i).getTemperature()); }