android - 改造 - 预期为 BEGIN_ARRAY 但实际为 BEGIN_OBJECT?

标签 android retrofit2 gson

我从服务中得到一个 json 结果,如下所示:

{
    "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 $

so change List<ProductsModel> to ProductsModel .

  • 如果 JSONJSONArray , 你可以将它解析为 List (如 List<ProductsModel> )。

  • 如果 JSONJSONObject , 你可以将它解析为 Object (如 ProductsModel )。

改成这个。

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

Call<ProductsModel> getAdminProductss();

关于android - 改造 - 预期为 BEGIN_ARRAY 但实际为 BEGIN_OBJECT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46754771/

相关文章:

android - 下载的 Facebook 应用程序替换了我的应用程序的 Facebook 登录页面

java - 请确认 .class 在 java 中的作用

android - 改造将我的字段 Int 转换为 String

java - 应为 BEGIN_ARRAY 但为 BEGIN_OBJECT retrofit2

java - 从数据库加载数据时的德语字符编码问题

java - 没有很多类的 GSON 解析

iphone - 我应该开始学习什么移动平台?

android - 在 Material 设计中设置弹出菜单

android - 改造 2 : How to set individual timeouts on specific requests?

java - 无法使用 GSON lib 反序列化对象