2

I am having a Json response returned by server, I created a model to represent this response with Kotlin data class. Part of the response is list of integers, and I want to serialize it in object with the same number of variables.

I am trying to model this JSON response

{
  "name": "My name",
  "uname": "UserName",
  "subs": [
    [
      8595622,
      49,
      30,
      0,
      1298408619,
      3,
      -1
    ],
    [
      8595636,
      49,
      30,
      0,
      1298409745,
      3,
      -1
    ]
  ]
}

The below code works fine in parsing

data class UserSubmission(

        @field:SerializedName("uname")
        val username: String? = null,

        @field:SerializedName("subs")
        val subs: List<List<Int?>?>? = null,

        @field:SerializedName("name")
        val name: String? = null
)

My problem is that I need the subs: List<List<Int?>?>? to be subs: List<List<Submission?>?>? Assuming Submission class is like that

class Submission {
      val x1: Int = 0,
      val x2: Int = 0,
      val x3: Int = 0,
      val x4: Int = 0,
      val x5: Int = 0,
      val x6: Int = 0,
      val x7: Int = 0
}
Esraa
  • 95
  • 12
  • 1
    If your Submission Class looks like that than you get all values into one class. So you don't need to have a List?>?. You need a List and you have to map your Values to the entries. Check this answer: https://stackoverflow.com/questions/43587973/how-to-deserialize-some-specific-fields-only-from-json-using-gson. But to me it looks like the JSON has a bad format if you want to have it in a class. – Erythrozyt May 29 '19 at 09:02
  • 2
    Write your own `TypeAdapter` which will read array and assign values to your object. – Miha_x64 May 29 '19 at 09:30

0 Answers0