使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException

在使用Gson解析Json时遇到JsonSyntaxException错误,问题源于Json对象嵌套时实体类类型定义错误。解决方案是确保实体类中嵌套对象的类型为下一级对象的实体类,而非List类型。此问题虽基础,但解决过程颇费周折,对于初学者具有一定的学习价值。

问题描述:使用Gson解析Json时,解析出错,无法映射为对象,报错提示:JsonSyntaxException: java.lang.IllegalStateException
解决途径:
1.查看报错信息,由于初学者,因此只看出来Gson解析有问题,打印http传回的json数据的确没问题。
2.搜索Gson的使用方法,看到别人做的好像很简单,建实体类,new一个Gson对象,用fromJson映射,做了多次无法解决问题。
3.直到搜到郭霖大神的volley用法详解的时候,其中有一章是自定义volley中的GsonRequest,写了天气中的小例子,我仔细观察发现Json多个嵌套对象的时候,嵌套对象的类型是其属性集合的类名,而不是我所认为的List类型`


public class Status {

    private int errNum;
    public int getErrNum() {
        return errNum;
    }
    public void setErrNum(int errNum) {
        this.errNum = errNum;
    }
    public String getErrMsg() {
        return errMsg;
    }
    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
    public RetData getRetData() {
        return retData;
    }
    public void setRetData(RetData retData) {
        this.retData =  retData;
    }
    private String errMsg;
    private RetData retData;

}
//嵌套对象的属性
public class RetData {


    private String city;
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getPinyin() {
        return pinyin;
    }
    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }
    public int getCitycode() {
        return citycode;
    }
    public void setCitycode(int citycode) {
        this.citycode = citycode;
    }
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public String getPostCode() {
        return postCode;
    }
    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }
    public float getLongiitude() {
        return longiitude;
    }
    public void setLongiitude(float longiitude) {
        this.longiitude = longiitude;
    }
    public float getLatitude() {
        return latitude;
    }
    public void setLatitude(float latitude) {
        this.latitude = latitude;
    }
    public int getAltitude() {
        return altitude;
    }
    public void setAltitude(int altitude) {
        this.altitude = altitude;
    }
    public String getWeather() {
        return weather;
    }
    public void setWeather(String weather) {
        this.weather = weather;
    }
    public int getTemp() {
        return temp;
    }
    public void setTemp(int temp) {
        this.temp = temp;
    }
    public String getLtemp() {
        return ltemp;
    }
    public void setLtemp(String ltemp) {
        this.ltemp = ltemp;
    }
    public String getHtemp() {
        return htemp;
    }
    public void setHtemp(String htemp) {
        this.htemp = htemp;
    }
    public String getWD() {
        return WD;
    }
    public void setWD(String wD) {
        WD = wD;
    }
    public String getWS() {
        return WS;
    }
    public void setWS(String wS) {
        WS = wS;
    }
    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;
    }
    private String pinyin;
    private int citycode;// 城市代码
    private String data;// 日期
    private String time;// 发布时间
    private String postCode;// 邮编
    private float longiitude;// 经度
    private float latitude;// 纬度
    private int altitude;// 海拔
    private String weather;// 天气情况
    private int temp;// 当前气温
    @SerializedName("l_temp")
    private String ltemp;// 最低气温
    @SerializedName("h_temp")
    private String htemp;// 最高气温
    private String WD;// 风向
    private String WS;// 风力
    private String sunrise;// 日出时间
    private String sunset;// 日落时间
}

问题结论:实体类中Json嵌套对象的类型为下一级对面实体类的类名。
虽然错误有点低级。。但是的确找了半天,积累了经验。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值