使用Gson解析的时候,遇到这个问题:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 21 path $.data
Json字符串是这样的:
{
"data": {
"banner": [
{
"aid": "7",
"catid": "0",
"description": "asd",
"end_time": "1432915200",
"imgurl": "/upload/ads/2015-05-06/5549ac6f05399.jpg",
"is_open": "1",
"link": "http://",
"order": "50",
"start_time": "1430755200",
"title": "asd"
}
],
"cat": [
{
"addtime": "1429063456",
"arrchildid": "1",
"arrparentid": "",
"catid": "1",
"catname": "母婴2",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "123",
"pid": "0",
"style": "0"
},
{
"addtime": "1429063835",
"arrchildid": "2,3,4,12,16",
"arrparentid": "",
"catid": "2",
"catname": "第三方",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
},
{
"addtime": "1429068195",
"arrchildid": "6,14",
"arrparentid": "",
"catid": "6",
"catname": "母婴1",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
},
{
"addtime": "1429068572",
"arrchildid": "7",
"arrparentid": "",
"catid": "7",
"catname": "母婴",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
},
{
"addtime": "1429068577",
"arrchildid": "8",
"arrparentid": "",
"catid": "8",
"catname": "母婴水电费",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
},
{
"addtime": "1429153032",
"arrchildid": "11,13",
"arrparentid": "",
"catid": "11",
"catname": "水电费水电费",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
},
{
"addtime": "1430117866",
"arrchildid": "15",
"arrparentid": "",
"catid": "15",
"catname": "SDFG",
"child": "0",
"image": "",
"is_show": "1",
"listorder": "0",
"pid": "0",
"style": "0"
}
],
"filter": {
"catid": 0,
"keywords": ""
},
"goods": [
{
"addtime": "123123123",
"brokerage": "600.00",
"catid": "2",
"collection": "0",
"content": "",
"description": "",
"id": "4",
"images": "",
"is_delete": "0",
"is_recommend": "1",
"is_sale": "1",
"keywords": "",
"listorder": "12",
"market_price": "1500.00",
"on_sale": "0",
"price": "1200.00",
"sales": "0",
"spec_array": "",
"status": "1",
"store": "120",
"title": "我是商品1",
"userid": "2"
}
]
},
"status": 0
}
使用HiJson格式化是这样的:
我的MallBean.java是这样写的
public class MallBean {
public List<MallData> data;
public int status;
public class MallData{
public List<MallDataBanner> banner;
public List<MallDataCat> cat;
public MallDataFilter filter;
public List<MallDataGoods> goods;
}
public class MallDataBanner{
public String aid;
public String catid;
public String description;
public String end_time;
public String imgurl;
public String is_open;
public String link;
public String order;
public String start_time;
public String title;
}
public class MallDataCat{
public String addtime;
public String arrchildid;
public String arrparentid;
public String catid;
public String catname;
public String child;
public String image;
public String is_show;
public String listorder;
public String pid;
public String style;
}
public class MallDataFilter{
public int catid;
public String keywords;
}
public class MallDataGoods{
public String addtime;
public String brokerage;
public String catid;
public String collection;
public String content;
public String description;
public String id;
public String images;
public String is_delete;
public String is_recommend;
public String is_sale;
public String keywords;
public String listorder;
public String market_price;
public String on_sale;
public String price;
public String sales;
public String status;
public String store;
public String title;
public String userid;
}
}
我解析是这样的(一下三种方法都是报同样的错):
Gson gson = new Gson();
MallBean mallBeanGson = gson.fromJson(response, MallBean.class);
// MallBean mallBeanGson = gson.fromJson(response, new TypeToken<MallBean>(){}.getType());
// List<MallBean> mallBeanGson = gson.fromJson(response, new TypeToken<List<MallBean>>(){}.getType());
,是bean对象写错了
public class MallBean { public List data; public int status; ...}MallBean中的data是一个对象,而不是数组, 所以应该改为:public MallData data;
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。