改型-预期为BEGIN_ARRAY,但为BEGIN_OBJECT? [英] Retrofit - Expected BEGIN_ARRAY but was BEGIN_OBJECT?

查看:97
本文介绍了改型-预期为BEGIN_ARRAY,但为BEGIN_OBJECT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过类似波纹管的翻修得到了json的结果:

I am getting a json result from service with retrofit like bellow :

{
    "result": {
        "totalCount": 15,
        "resultCount": 2,
        "offset": 0,
        "limit": 2,
        "products": [
            {
                "id": 10081,
                "name": "prod",
                "pictureUrl": "url",
                "price": 1,
                "url": "url",
                "briefDescription": "test",
                "description": "test",
                "pictures": [],
                "categoryTitle": "s s",
                "categoryId": 53,
                "extraInfo": {
                    "productProperties": [
                        {
                            "id": 88,
                            "value": "6",
                            "measurementUnit": "s",
                            "title": "s"
                        },
                        {
                            "id": 89,
                            "value": "2",
                            "measurementUnit": "s",
                            "title": "s s"
                        },
                        {
                            "id": 90,
                            "value": "2",
                            "measurementUnit": "s",
                            "title": "s s s s"
                        },
                        {
                            "id": 91,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s s"
                        },
                        {
                            "id": 92,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s s"
                        },
                        {
                            "id": 93,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s"
                        },
                        {
                            "id": 94,
                            "value": "",
                            "measurementUnit": "",
                            "title": "s"
                        }
                    ],
                    "published": false,
                    "preparationTime": 1,
                    "keywords": "",
                    "quantity": 0,
                    "status": 1
                }
            },
            {
                "id": 51,
                "name": "nam3",
                "pictureUrl": "url",
                "price": 495000,
                "url": "url",
                "briefDescription": "sdsds",
                "description": "-",
                "pictures": [],
                "categoryTitle": "x  x x",
                "categoryId": 179,
                "extraInfo": {
                    "productProperties": [
                        {
                            "id": 67,
                            "value": "1000",
                            "measurementUnit": "x",
                            "title": "x x"
                        },
                        {
                            "id": 68,
                            "value": "1050",
                            "measurementUnit": "s",
                            "title": "x x x"
                        },
                        {
                            "id": 69,
                            "value": "",
                            "measurementUnit": "",
                            "title": "x x"
                        },
                        {
                            "id": 70,
                            "value": "",
                            "measurementUnit": "",
                            "title": "x x"
                        },
                        {
                            "id": 71,
                            "value": "",
                            "measurementUnit": "",
                            "title": "xxxx"
                        }
                    ],
                    "published": true,
                    "preparationTime": 2,
                    "keywords": "Aswddfe",
                    "quantity": 93,
                    "status": 1
                }
            }
        ]
    }
} 

我变得像retrofit一样吼叫:

RetrofitApi.getVendorAdminApi()
        .getAdminProductss(userToken, limit, pageNumber, filters)
        .enqueue(new Callback<List<ProductsModel>>() {
            @Override
            public void onResponse(Call<List<ProductsModel>> call, Response<List<ProductsModel>> response) {
                if (response.isSuccessful()) {
                    resultListener.onSuccess(response.body());
                } else {
                    resultListener.onFailure();
                }
            }

            @Override
            public void onFailure(Call<List<ProductsModel>> call, Throwable t) {
                resultListener.onFailure();
                t.printStackTrace();
            }
        });

但是说我:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

下面是我的模特:

public class ProductsModel {

    @SerializedName("result")
    @Expose
    private ResultProducts result;

    public ResultProducts getResult() {
        return result;
    }

    public void setResult(ResultProducts result) {
        this.result = result;
    }

}

推荐答案

您的问题是Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

因此将List<ProductsModel>更改为ProductsModel.

  • 如果JSONJSONArray,则可以将其解析为List(如List<ProductsModel>).

    • If JSON is JSONArray,you can parse it to List (like List<ProductsModel>).

      如果JSONJSONObject,则可以将其解析为Object(如ProductsModel).

      If JSON is JSONObject,you can parse it to Object (like ProductsModel).

      更改为此.

      @Override
      public void onResponse(Call<ProductsModel> call, Response<ProductsModel> response) {
          if (response.isSuccessful()) {
              resultListener.onSuccess(response.body());
          } else {
              resultListener.onFailure();
          }
      }
      

      还有

      Call<ProductsModel> getAdminProductss();
      

      这篇关于改型-预期为BEGIN_ARRAY,但为BEGIN_OBJECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆