首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

GSON解析JSON数据解决思路

2014-01-15 
GSON解析JSON数据{resultcode:200,reason:查询成功!,result:{sk:{temp:11,wind_directio

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) {


this.week = week;
}
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 getDressing_index() {
return dressing_index;
}
public void setDressing_index(String dressing_index) {
this.dressing_index = dressing_index;
}
public String getDressing_advice() {
return dressing_advice;
}
public void setDressing_advice(String dressing_advice) {
this.dressing_advice = dressing_advice;
}
public String getUv_index() {
return uv_index;
}
public void setUv_index(String uv_index) {
this.uv_index = uv_index;
}
public String getComfort_index() {
return comfort_index;
}
public void setComfort_index(String comfort_index) {
this.comfort_index = comfort_index;
}
public String getTravel_index() {
return travel_index;
}
public void setTravel_index(String travel_index) {
this.travel_index = travel_index;
}
public String getExercise_index() {
return exercise_index;
}
public void setExercise_index(String exercise_index) {
this.exercise_index = exercise_index;
}
public String getDrying_index() {
return drying_index;
}
public void setDrying_index(String drying_index) {
this.drying_index = drying_index;
}
    }
    class Sk{
    private String temp="";
    private String wind_direction="";
    private String wind_strength="";
    private String humidity="";
    private String time="";
public String getTemp() {
return temp;
}
public void setTemp(String temp) {
this.temp = temp;
}
public String getWind_direction() {
return wind_direction;
}
public void setWind_direction(String wind_direction) {
this.wind_direction = wind_direction;
}
public String getWind_strength() {
return wind_strength;
}
public void setWind_strength(String wind_strength) {
this.wind_strength = wind_strength;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
    }
    }

public String getResultcode() {
return resultcode;
}

public void setResultcode(String resultcode) {
this.resultcode = resultcode;
}

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public Result getResult() {
return result;
}

public void setResult(Result result) {
this.result = result;
}
    

}


这个是将字符串转成上边对象的方法:
public static <T> T decodeObject(String json, Class<T> clz) {
JSONObject jsonObject = JSONObject.fromObject(json);
T bean = (T) JSONObject.toBean(jsonObject, clz);
return bean;
}

你json字符串太长了 ,没测试。

热点排行