Android - 改造异常预期 BEGIN_OBJECT 但 BEGIN_ARRAY

Android - Retrofit exception Expected BEGIN_OBJECT but was BEGIN_ARRAY

我是 GSON 的新手和改造者,这是我的输出模型(仅供参考结构),

    public class Result1
    {
        public int TotalCount { get; set; }
    }

    public class Result2
    {
        public int PostId { get; set; }
        public int PostTypeId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool IsFeatured { get; set; }
        public string Thumbnail { get; set; }
        public string CreatedDate { get; set; }
        public int CreatedBy { get; set; }
    }



public class RootObject
  {
     @Expose
        private List<com.example.got_sample.Result2> Result2 = new ArrayList<com.example.got_sample.Result2>();

    public List<com.example.got_sample.Result1> getResult1() {
        return Result1;
        }

        public void setResult1(List<com.example.got_sample.Result1> Result1) {
        this.Result1 = Result1;
        }

        public List<com.example.got_sample.Result2> getResult2() {
        return Result2;
        }
        public void setResult2(List<com.example.got_sample.Result2> Result2) {
        this.Result2 = Result2;
        }
    //Similar for result1
            }

这是我的输出

    [{"Result1":

[{"TotalCount":5}]},

    {"Result2":

    [{"PostId":6,"PostTypeId":1,"Title":"","Description":"something"..},{"PostId":7,"PostTypeId":1,"Title":"","Description":"something"..}]
    }]

这是我的改装代码

 postmethod.sendpostrequest(object, new Callback<RootObject>() {
                      @Override
                      public void failure(RetrofitError retrofitError) {
                          System.out.println(retrofitError.getMessage());
                      }

                    @Override
                    public void success(RootObject arg0,
                            retrofit.client.Response arg1) {
                        // TODO Auto-generated method stub

                    }
                  });

    This is the exception I get"

Expected BEGIN_OBJECT but was BEGIN_ARRAY"

我尝试用 List < Result2 > 替换 RootObject,因为我只对结果感兴趣 2.In 在这种情况下,我得到的响应是 null.Please 帮助。

您的 JSON 的根是一个 Result1 对象数组,但您的回调方法是针对单个 RootObject 的。

尝试修改您的 JSON 以“{”开头并以“}”结尾,使根成为对象而不是数组。