GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY

时间:2017-12-30 07:20:33

标签: kotlin gson

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

  

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)

2 个答案:

答案 0 :(得分:0)

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

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

您有两种选择:

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

希望有所帮助!

答案 1 :(得分:0)

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

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

PS。如果您的数据类包含equals(),建议您实施自己的hashCode()Array函数,因为Array的{​​{1}}函数的默认实现会比较{ {3}}。建议阅读:referential equality