我得到了一个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;
}
}
5条答案
按热度按时间xu3bshqb1#
你的问题是
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
因此将
List<ProductsModel>
更改为ProductsModel
。JSON
是JSONArray
,则可以将其解析为List
(类似于List<ProductsModel>
)。JSON
是JSONObject
,则可以将其解析为Object
(类似于ProductsModel
)。换这个。
还有
qnyhuwrf2#
将您的Retrofit呼叫更改为:
原因为:如果您输入
List<ProductsModel>
,它将检查JsonArray
,但在您的响应中,它将返回JsonObject
,因此只需将List<ProductsModel>
更改为ProductsModel
即可解决您的问题。希望能有所帮助!
1yjd4xko3#
json数据中的“result”节点是一个对象,而不是数组,json的model类如下
回叫将是
Callback<ResponseJSON>
可以通过response.body().getResult().getProducts()得到产品结果;
我已经从http://pojo.sodhanalibrary.com/生成了模型。如果你想的话,你可以通过引用我给出的模型来创建另一个模型。
gcmastyq4#
作为服务响应,您有一个具有以下键的OBJECT:“result”,包含一些字段和一个LIST OF PRODUCT(数组,关键字为“products”)。您的
ProductsModel
相当不错。但是您的改造实现是错误的,应该是:变更:将所有
List<ProductsModel>
替换为ProductsModel
。lvmkulzt5#
两者之一(后端)
更改您的API答复
从
至
或(安卓)
更改您的翻新服务
从
至