【问题标题】:Expected a string but was BEGIN_OBJECT error应为字符串,但出现 BEGIN_OBJECT 错误
【发布时间】:2018-04-21 08:42:10
【问题描述】:

我有以下 JSON、代码和模型:

这是 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"
}

这是retrofitCall方法

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、我的代码还是我的模型?

【问题讨论】:

标签: android json retrofit


【解决方案1】:

在您的 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...
}

然后你可以在你的ListModel 中使用InvoiceDetailModel invoice_detail 而不是String invoice_detail

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多