我有以下JSON,代码和模型:
{
"success": "false",
"http": "ok",
"status_code": "200",
"invoice_detail": {
"notifications": [
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
},
{
"quantity": "414",
"price": "5412",
"total_price": "15748",
"name": "Axel_Item_5412"
}
]
},
"sgst": "125478122"
}
private void retrofitCall() {
String url= "http://www.amock.io/api/imran.cyber/get-fake";
Call<ListModel> call= apiInterface.getList("", url);
call.enqueue(new Callback<ListModel>() {
@Override
public void onResponse(Call<ListModel> call, Response<ListModel> response) {
if (response.isSuccessful()){
String s= response.body().getInvoice_detail();
Log.e(TAG, "onResponse: "+s);
}
}
@Override
public void onFailure(Call<ListModel> call, Throwable t) {
Log.e(TAG, "onFailure: "+t.getMessage());
}
});
}
public class ListModel {
@SerializedName("invoice_detail")
public String invoice_detail;
public ListModel(String invoice_detail) {
this.invoice_detail = invoice_detail;
}
public String getInvoice_detail() {
return invoice_detail;
}
public void setInvoice_detail(String invoice_detail) {
this.invoice_detail = invoice_detail;
}
我收到了这个错误:
11-08 10:09:18.489 3578-3578 /? E / MainActivity :: onFailure:java.lang.IllegalStateException:期望一个字符串,但在第5行第22行路径为$ .invoice_detail
的BEGIN_OBJECT
我不确定出了什么问题:我的JSON,我的代码还是我的模特?
答案 0 :(得分:2)
在您的ListModel
中声明字段String invoice_detail
,但在您的JSON中,invoice_detail
是另一个对象,而不是字符串,因此您会收到该错误消息。
如果要完全解析JSON,则需要定义另外两个模型来处理发票。包含通知的发票明细:
public class InvoiceDetailModel {
public InvoiceNotificationModel[] notifications;
public InvoiceNotificationModel[] getNotifications() {
return notifications;
}
}
以及通知本身的模型:
public class InvoiceNotificationModel {
public String quantity;
public String price;
public String total_price;
public String name;
// any getter methods here...
}
然后,您可以在InvoiceDetailModel invoice_detail
中使用String invoice_detail
代替ListModel
。