当前位置: 代码迷 >> Java相关 >> GSON解析JSON数据解决思路
  详细解决方案

GSON解析JSON数据解决思路

热度:106   发布时间:2016-04-22 21:24:03.0
GSON解析JSON数据
{
"resultcode":"200",
"reason":"查询成功!",
"result":
{
"sk":
{"temp":"11","wind_direction":"东北风","wind_strength":"3级","humidity":"60%","time":"10:05"},
"today":
{"city":"深圳","date_y":"2014年01月14日","week":"星期二","temperature":"7~17","weather":"多云","wind":"无持续风向微风","dressing_index":"冷","dressing_advice":"天气冷,建议着棉服、羽绒服、皮夹克加羊毛衫等冬季服装。年老体弱者宜着厚棉衣、冬大衣或厚羽绒服。","uv_index":"弱","comfort_index":"","wash_index":"适宜","travel_index":"适宜","exercise_index":"较适宜","drying_index":""},
"future":
{
"day_20140114":{"temperature":"7~17","weather":"多云","wind":"无持续风向微风","week":"星期二","date":"20140114"},
"day_20140115":{"temperature":"7~16","weather":"晴","wind":"无持续风向微风","week":"星期三","date":"20140115"},
"day_20140116":{"temperature":"8~18","weather":"晴","wind":"无持续风向微风","week":"星期四","date":"20140116"},
"day_20140117":{"temperature":"10~19","weather":"晴","wind":"无持续风向微风","week":"星期五","date":"20140117"},
"day_20140118":{"temperature":"8~17","weather":"晴转多云","wind":"无持续风向微风","week":"星期六","date":"20140118"},
"day_20140119":{"temperature":"10~16","weather":"多云转阴","wind":"无持续风向微风","week":"星期日","date":"20140119"},
"day_20140120":{"temperature":"11~17","weather":"阴","wind":"无持续风向微风","week":"星期一","date":"20140120"}
}
}
}
请问上面的JSON数据中future怎么解析成java对象,future下字段名每天都会改变,java对象怎么定义?GSON要求是对象必须与JSON字段同名;求大神知道;

------解决方案--------------------

bean对象:
import java.util.HashMap;
import java.util.Map;
public class Test{
     String resultcode="";
     String reason="";
     Result result=new Result();
    
     class Result{
     Sk sk=new Sk();
     Today today=new Today();
     Map<String, Day> future=new HashMap<String, Day>();
     class Day{
     private String temperature="";
     private String weather="";
     private String wind="";
     private String week="";
     private String date="";
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;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
     }
     class Today{
     private String city="";
     private String date_y="";
     private String week="";
     private String temperature="";
     private String weather="";
     private String wind="";
     private String dressing_index="";
     private String dressing_advice="";
     private String uv_index="";
     private String comfort_index="";
     private String wash_index="";
     private String travel_index="";
     private String exercise_index="";
     private String drying_index="";
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDate_y() {
return date_y;
}
public void setDate_y(String date_y) {
this.date_y = date_y;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
  相关解决方案