Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit - Expected BEGIN_ARRAY but was BEGIN_OBJECT?

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
                }
            }
        ]
    }
} 

And I am getting like bellow with 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();
            }
        });

But say me :

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

And bellow is my model :

public class ProductsModel {

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

    public ResultProducts getResult() {
        return result;
    }

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

}

1 Answers

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

so change List<ProductsModel> to ProductsModel .

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

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

Change to this.

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

And

Call<ProductsModel> getAdminProductss();
like image 193
KeLiuyue Avatar answered Dec 24 '25 04:12

KeLiuyue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!