gson解析错误
想要解析的json字符串是这样的:{"data":{"countcommenturl":"http://zhbj.qianlong.com/client/content/...
想要解析的json字符串是这样的:
{
"data": {
"countcommenturl": "http://zhbj.qianlong.com/client/content/countComment/",
"more": "http://zhbj.qianlong.com/static/api/news/10003/list_2.json",
"news": [
{
"comment": true,
"commentlist": "http://zhbj.qianlong.com/static/api/news/10003/72/82772/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/82772",
"id": 82772,
"largeimage": "http://zhbj.qianlong.com/static/images/2014/11/07/70/476518773M7R.jpg",
"listimage": "http://192.168.6.188:8080/zhbj/photos/images/46728356JDGO.jpg",
"pubdate": "2014-11-07 11:40",
"smallimage": "http://zhbj.qianlong.com/static/images/2014/11/07/79/485753989TVL.jpg",
"title": "北京·APEC绚丽之夜",
"type": "news",
"url": "http://zhbj.qianlong.com/static/html/2014/11/07/7743665E4E6B10766F26.html"
},
],
"title": "组图",
"topic": []
},
"retcode": 200
}
封装的对象是这样的 :
public class PhotosBean {
public String retcode;
public Photos data;
public class Photos {
public String countcommenturl;
public String more;
public List<PhotosItem> news;
public String title;
public List topic;
}
public class PhotosItem {
public String comment;
public String commentlist;
public String commenturl;
public String id;
public String largeimage;
//显示图片的url地址
public String listimage;
public String pubdate;
public String smallimage;
//该图片的标题
public String title;
public String type;
public String url;
}
}
将json string 转成一个对象
Gson gson=new Gson();
photosBean = gson.fromJson(cacheJson, PhotosBean.class);
但却报了这样一个异常:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
该怎样解决啊,请给建议。 展开
{
"data": {
"countcommenturl": "http://zhbj.qianlong.com/client/content/countComment/",
"more": "http://zhbj.qianlong.com/static/api/news/10003/list_2.json",
"news": [
{
"comment": true,
"commentlist": "http://zhbj.qianlong.com/static/api/news/10003/72/82772/comment_1.json",
"commenturl": "http://zhbj.qianlong.com/client/user/newComment/82772",
"id": 82772,
"largeimage": "http://zhbj.qianlong.com/static/images/2014/11/07/70/476518773M7R.jpg",
"listimage": "http://192.168.6.188:8080/zhbj/photos/images/46728356JDGO.jpg",
"pubdate": "2014-11-07 11:40",
"smallimage": "http://zhbj.qianlong.com/static/images/2014/11/07/79/485753989TVL.jpg",
"title": "北京·APEC绚丽之夜",
"type": "news",
"url": "http://zhbj.qianlong.com/static/html/2014/11/07/7743665E4E6B10766F26.html"
},
],
"title": "组图",
"topic": []
},
"retcode": 200
}
封装的对象是这样的 :
public class PhotosBean {
public String retcode;
public Photos data;
public class Photos {
public String countcommenturl;
public String more;
public List<PhotosItem> news;
public String title;
public List topic;
}
public class PhotosItem {
public String comment;
public String commentlist;
public String commenturl;
public String id;
public String largeimage;
//显示图片的url地址
public String listimage;
public String pubdate;
public String smallimage;
//该图片的标题
public String title;
public String type;
public String url;
}
}
将json string 转成一个对象
Gson gson=new Gson();
photosBean = gson.fromJson(cacheJson, PhotosBean.class);
但却报了这样一个异常:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY
该怎样解决啊,请给建议。 展开
2个回答
展开全部
看翻译是new解析错误,因为你的json字符串中只有一个数组,和news是对应的。
可以把public List<PhotosItem> news;改为public PhotosItem[] news;试试。
gson不是很熟。我一般用下面的两个:
fastjson:淘宝开源json库,挺好用的。尤其对于较小json数据。
jackson:国外的一个,对于超大的json字符串,我一般用这个,因为其有流模式的解析api。
可以把public List<PhotosItem> news;改为public PhotosItem[] news;试试。
gson不是很熟。我一般用下面的两个:
fastjson:淘宝开源json库,挺好用的。尤其对于较小json数据。
jackson:国外的一个,对于超大的json字符串,我一般用这个,因为其有流模式的解析api。
追问
多个的,篇幅限制我只截取一部分显示的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询