這是
baidu weather api如何使用GSON解析來自百度天氣API的JSON
URL中的JSON是
{
"error": 0,
"status": "success",
"date": "2015-10-02",
"results": [
{
"currentCity": "beijing",
"pm25": "81",
"index": [
{
"title": "穿衣",
"zs": "舒適",
"tipt": "穿衣指數",
"des": "建議着長袖T恤、襯衫加單褲等服裝。年老體弱者宜着針織長袖襯衫、馬甲和長褲。"
},
{
"title": "洗車",
"zs": "較適宜",
"tipt": "洗車指數",
"des": "較適宜洗車,未來一天無雨,風力較小,擦洗一新的汽車至少能保持一天。"
},
{
"title": "旅遊",
"zs": "適宜",
"tipt": "旅遊指數",
"des": "天氣較好,溫度適宜,是個好天氣哦。這樣的天氣適宜旅遊,您可以盡情地享受大自然的風光。"
},
{
"title": "感冒",
"zs": "少發",
"tipt": "感冒指數",
"des": "各項氣象條件適宜,無明顯降溫過程,發生感冒機率較低。"
},
{
"title": "運動",
"zs": "較適宜",
"tipt": "運動指數",
"des": "天氣較好,戶外運動請注意防曬。推薦您進行室內運動。"
},
{
"title": "紫外線強度",
"zs": "強",
"tipt": "紫外線強度指數",
"des": "紫外線輻射強,建議塗擦SPF20左右、PA++的防曬護膚品。避免在10點至14點暴露於日光下。"
}
],
"weather_data": [
{
"date": "週五 10月02日 (實時:18℃)",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微風",
"temperature": "26 ~ 12℃"
},
{
"date": "週六",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微風",
"temperature": "25 ~ 12℃"
},
{
"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": "微風",
"temperature": "25 ~ 13℃"
},
{
"date": "週一",
"dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
"nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
"weather": "晴",
"wind": "微風",
"temperature": "26 ~ 13℃"
}
]
}
]
}
我想用GSON解析它,但我遇到了一些問題。
在我WeatherInfo.java 我寫它像
public class WeatherInfo
{
private String error;
private String status;
private String date;
public String getError() {return error;}
public void setError(String error) {this.error = error;}
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 Results results;
public Results getResults(){return results;}
public void setResults(Results results){this.results = results;}
public static class Results
{
public String currentCity;
public String pm25;
public String getCurrentCity(){return currentCity;}
public void setCurrentCity(String currentCity){this.currentCity = currentCity;}
public String getPm25(){return pm25;}
public void setPm25(String pm25){this.pm25 = pm25;}
public Index getIndex() {
return index;
}
public void setIndex(Index index) {
this.index = index;
}
public Index index;
public Weather_data getWeather_data() {
return weather_data;
}
public void setWeather_data(Weather_data weather_data) {
this.weather_data = weather_data;
}
public Weather_data weather_data;
public static class Index
{
private String title;
private String zs;
private String des;
private String tipt;
public String getTitle(){return title;}
public void setTitle(String title){this.title = title;}
public String getZs(){return zs;}
public void setZs(String zs){this.zs = zs;}
public String getTipt() {return tipt;}
public void setTipt(String tipt) {this.tipt = tipt;}
public String getDes() {return des;}
public void setDes(String des) {this.des = des;}
}
public class Weather_data
{
private String data;
private String dayPictureUrl;
private String nightPictureUrl;
private String weather;
private String wind;
private String temperature;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getDayPictureUrl() {
return dayPictureUrl;
}
public void setDayPictureUrl(String dayPictureUrl) {
this.dayPictureUrl = dayPictureUrl;
}
public String getNightPictureUrl() {
return nightPictureUrl;
}
public void setNightPictureUrl(String nightPictureUrl) {
this.nightPictureUrl = nightPictureUrl;
}
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 getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
}
}
}
我使用這個功能來解析JSON
public void parseJsonWithGson(String jsonData) {
Log.d(TAG,"用gson進行解析");
Gson gson = new Gson();
WeatherInfo weatherInfo = gson.fromJson(jsonData,WeatherInfo.class);
Log.d(TAG,"weatherInfo"+weatherInfo);
Log.d(TAG,"error is "+weatherInfo.getError());
Log.d(TAG,"status is "+weatherInfo.getStatus());
Log.d(TAG,"date is "+weatherInfo.getDate());
Log.d(TAG,"currentcity is "+weatherInfo.getResults().getCurrentCity());
}
最後有一個錯誤的
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 62 path $.results
錯誤的含義正是它在消息中陳述的內容...... – Selvin