开发者社区> 问答> 正文

Gson : Expected BEGIN_ARRAY but was BEGIN_OBJECT

使用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格式化是这样的:
screenshot
我的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());

展开
收起
杨冬芳 2016-07-05 09:16:34 7384 0
1 条回答
写回答
取消 提交回答
  • IT从业

    ,是bean对象写错了
    public class MallBean { public List data; public int status; ...}MallBean中的data是一个对象,而不是数组, 所以应该改为:public MallData data;

    2019-07-17 19:51:09
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Tracking-Ransomware-End-To-End 立即下载
The-Future-Of-Applepwn-How-To-Save-Your-Money 立即下载
低代码开发师(初级)实战教程 立即下载