GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY相关的知识,希望对你有一定的参考价值。

这几天面对这个问题。有没有人检查过这个问题?任何帮助将不胜感激。我怎么能解决这个问题?

GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY

问题来自

 override fun onSuccess(str_SUCCESS: String)
    {
        System.out.println("JSON_IS"+str_SUCCESS)

       val paymentScheduleModel = Gson().fromJson<PaymentScheduleModel>(str_SUCCESS, PaymentScheduleModel::class.java) // Problem here

    }

Json Response是

{
  "status": {
    "statusCode": 10016,
    "isSuccess": true,
    "message": "Success"
  },
  "data": {
    "payback_schedule": [
      {
        "id": 2,
        "paid_amount": "INR NaN",
        "paidStatus": "Upcoming Payback",
        "paid_status": "P",
        "s_date": "05/01/2018 12:31:10",
        "e_date": "11/01/2018 12:31:10",
        "current_balance": "INR 399",
        "payanytime_button_status": "active",
        "btnColor": "red",
        "btnHexColor": "#D2322D"
      },
      {
        "id": 3,
        "paid_amount": "INR NaN",
        "paidStatus": "Upcoming Payback",
        "paid_status": "P",
        "s_date": "12/01/2018 12:31:10",
        "e_date": "18/01/2018 12:31:10",
        "current_balance": "INR 399",
        "payanytime_button_status": "active",
        "btnColor": "red",
        "btnHexColor": "#D2322D"
      }
    ]
  }
}

PaymentScheduleModel

data class PaymentScheduleModel(@SerializedName("payback_schedule") val payback_schedule: PaymentSchedule)

data class PaymentSchedule

                      (@SerializedName("id") val id: Int,
                       @SerializedName("paid_amount") val paid_amount:String,
                       @SerializedName("paidStatus") val paidStatus:String,
                       @SerializedName("paid_status") val paid_status:String,
                       @SerializedName("s_date") val s_date:String,
                       @SerializedName("e_date") val e_date:String,
                       @SerializedName("current_balance") val current_balance:String,
                       @SerializedName("payanytime_button_status") val payanytime_button_status:String,
                       @SerializedName("btnColor") val btnColor:String,
                       @SerializedName("btnHexColor") val btnHexColor:String)
答案

您的模型对象与您的Json不匹配。

您正在尝试解析具有PaymentScheduleModel类型的子对象“payback_schedule”的JsonObject PaymentSchedule,但是您有一个JsonObject,它具有子对象“data”,它具有子对象“payback_schedule”。所以,你真的要解析“数据”块。

您有两种选择:

1:创建另一个包装数据块的模型并解析:

data class PaymentScheduleData(@SerializedName("data") val payback_schedule_model: PaymentScheduleModel)

override fun onSuccess(str_SUCCESS: String) {
    val paymentScheduleData = Gson().fromJson<PaymentScheduleData>(str_SUCCESS, PaymentScheduleData::class.java)
    // Now use paymentScheduleData.payback_schedule_model
}

2:首先拉出数据部分,然后解析:

override fun onSuccess(str_SUCCESS: String) {
    // Get the root JsonObject
    val jsonObject = Gson().fromJson<JsonObject>(str_SUCCESS, JsonObject::class.java)

    // Get the "data" block that matches the model and parse that
    val paymentScheduleModel = Gson().fromJson<PaymentScheduleModel>(jsonObject.getAsJsonObject("data"), PaymentScheduleModel::class.java)
}

希望有所帮助!

另一答案

错误告诉你payback_schedule持有数组而不是对象。因此,payback_schedule应该是Array<PaymentSchedule>而不是PaymentSchedule

data class PaymentScheduleModel(@SerializedName("payback_schedule") val payback_schedule: Array<PaymentSchedule>)

PS。如果您的数据类包含equals(),建议您实现自己的hashCode()Array函数,因为Arrayequals()函数的默认实现比较了referential equality。建议阅读:Equals method for data class in kotlin

以上是关于GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY的主要内容,如果未能解决你的问题,请参考以下文章

更改我的程序接受文件的方式将从GSON创建此错误:预期BEGIN_ARRAY但在第1行第2列路径$ BEGIN_OBJECT?

“预期为 BEGIN_OBJECT,但在第 1 行第 1 列为 STRING”

预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT

我怎样才能得到 Gson 的解析?我尝试这样做,但我得到了一个 Expected BEGIN_ARRAY 但是是 BEGIN_OBJECT

GSON 应为 BEGIN_ARRAY,但为 BEGIN_OBJECT

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEG