Kotlin | Jackson annotation | How to fix Out of START_ARRAY token Error

Viewed 297

Can anybody say where I am doing wrong. I have json like that

[
  {
    "id": "1",
    "name": "ff",
    "surname": "ggg",
    "cap": "10000"
  },
  {
    "id": "1",
    "name": "aaa",
    "surname": "hhh",
    "cap": "22222"
  },
  {
    "id": "1",
    "name": "rrr",
    "surname": "hhhhhdr",
    "cap": "33333"
  },
  {
    "id": "1",
    "name": "hhh",
    "surname": "qqqqq",
    "cap": "44444"
  }
]

And I parse to this class.

data class ResponseList(
    val capList: List<Response>?
) {
    data class Response(
        @JsonProperty("id")
        val id: String,
        @JsonProperty("name")
        val name: String,
        @JsonProperty("surname")
        val surname: String,
        @JsonProperty("cap")
        val cap: String
    )
}

When I try to parse it the list is always null and if I try to test it I have this error: Cannot deserialize value of type com.myapp.ResponseList from Array value (token JsonToken.START_ARRAY)

1 Answers

just class Response is needed, like following:

fun test(){
    val jsonStr = "your json str"
    val mapper = ObjectMapper()
    val lendReco: List<Response> =
        mapper.readValue(jsonStr, object : TypeReference<List<Response?>?>() {})
}

data class Response(
    @JsonProperty("id")
    val id: String,
    @JsonProperty("name")
    val name: String,
    @JsonProperty("surname")
    val surname: String,
    @JsonProperty("cap")
    val cap: String
)
Related