0

Facing this problem past few days.Does any guys check this issue? Any help would be greatly appreciated.How can I solved this problem?

GSON throwing Expected BEGIN_OBJECT but was BEGIN_ARRAY

Problem coming from

 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 is

{
  "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)
A B
  • 33
  • 9

2 Answers2

0

Your model object does not match your Json.

You are trying to parse a JsonObject PaymentScheduleModel which has sub object "payback_schedule" of type PaymentSchedule but you have a JsonObject which has a sub object "data" which is what has the sub object "payback_schedule". So really, you want to parse the "data" block.

You have two options:

1: Create another model that wraps the data block and parse that:

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: Pull out the data portion first, then parse:

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

Hope that helps!

dominicoder
  • 9,338
  • 1
  • 26
  • 32
0

The error is telling you that payback_schedule is holding an array instead of object. So, payback_schedule should be Array<PaymentSchedule> instead of PaymentSchedule.

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

PS. You are suggested to implement your own equals() and hashCode() function if your data class contains Array because the default implementation of Array's equals() function compares the referential equality. Suggested reading: Equals method for data class in kotlin

BakaWaii
  • 6,732
  • 4
  • 29
  • 41