使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException
来源:互联网 发布:北大青鸟java视频教程 编辑:程序博客网 时间:2024/05/29 06:38
问题描述:使用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嵌套对象的类型为下一级对面实体类的类名。
虽然错误有点低级。。但是的确找了半天,积累了经验。
0 0
- 使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException
- com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常
- 错误 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: closed 解决方案
- Gson解析出现类似错误,com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_AR
- Gson解析出现类似错误,com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
- Gson解析出现类似错误,com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_AR
- Gson解析出现类似错误,com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_AR
- Gson解析出现类似错误;com.google.gson.JsonSyntaxException:java.lang.illegalStateException:Exception BEGIN_AR
- Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJE
- com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was B
- Gson - Java-JSON 序列化和反序列化入门
- Gson - Java-JSON 序列化和反序列化入门
- gson转JSON数据 报错java.lang.NullPointerException的解决方法
- mvc使用Newtonsoft.Json反序列化json数据
- Json工具类--使用Gson实现了Json的序列化和反序列化
- Gson转换报错com.google.gson.JsonSyntaxException
- spark报错:java反序列化
- Gson 反序列化
- 二分图-最大匹配,最小路径覆盖,最小点覆盖
- git学习
- android AsyncTask介绍
- codevs--1026 逃跑的拉尔夫
- VS 操作 word 出错 解决方案
- 使用Gson反序列化Json数据失败,报错JsonSyntaxException: java.lang.IllegalStateException
- Android Base64编码解码
- Metasploitable2使用指南
- 复习一下两列布局。
- 多态的作用——通用性
- CentOS 7.X设置自定义开机启动,添加自定义系统服务
- CodeForces 664B Rebus (调整策略)
- CSS学习
- 遗传算法