Gson 问题

helloha2013 helloha2013 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
假设与如下json,使用Gson读取

{
"code":100,
"griddata":[
{"img":"http:\/\/img.netbian.com\/file\/20140405\/fa129372a26730ea8030dbbb392d6ac7.jpg"},
{"img":"http:\/\/img.netbian.com\/file\/2016\/0529\/849bfad06e44e67364752eee6a382448.jpg"},
{"img":"http:\/\/img.netbian.com\/file\/20140405\/fa129372a26730ea8030dbbb392d6ac7.jpg"}
]
}

对应的bean是

public class TTTT {

/**
* code : 100
* griddata : [{"img":"http://img2.kanrenti.org/photoL/7254/01.jpg"},{"img":"http://img2.kanrenti.org/photoL/7254/02.jpg"},{"img":"http://img2.kanrenti.org/photoL/7254/03.jpg"}]
*/

private int code;
/**
* img : http://img2.kanrenti.org/photoL/7254/01.jpg
*/

private List<GriddataEntity> griddata;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public List<GriddataEntity> getGriddata() {
return griddata;
}

public void setGriddata(List<GriddataEntity> griddata) {
this.griddata = griddata;
}

public static class GriddataEntity {
private String img;

public String getImg() {
return img;
}

public void setImg(String img) {
this.img = img;
}
}
}


然后使用Gson的fromJson方法读取,如
Type lt = new TypeToken<List<TTTT>>() {}.getType();
List<TTTT> tmp1 = g.fromJson(response, lt); //此句报错
或者
TTTT tmp1=g.fromJson(response,TTTT.class); //此句报错

都报:Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
请问,Gson如何读取这样啦的bean?
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
helloha2013
Android 80,489 成员 91,375 内容