1

Maybe this is simple, but I'm missing how to do this. I'm using GSON, kotlin, and retrofit

Data.json

{
  "array1":[1,2],
  "array2":[1,2]
}

DataObject.kt

data class DataObject(array1: List<Int>, array2: List<Int>)

The the above fails to deserialize the arrays.

1
  • Considering the kotlin code you've provided is not valid and wouldnt compile, I don't see how this code would even get to the deserialization step. Commented Apr 10, 2018 at 22:14

1 Answer 1

0

This is a running example:

data class DataObject(val array1: List<Int>, val array2: List<Int>)

fun main(args: Array<String>) {
    val json = """{"array1":[1,2],"array2":[1,2]}"""
    println(Gson().fromJson(json, DataObject::class.java))
    //DataObject(array1=[1, 2], array2=[1, 2])
}

I've only changed the arguments of DataObject to be val, which is required for data classes (var also works).

Sign up to request clarification or add additional context in comments.

1 Comment

The properties of a data class may also be var.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.