问题
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:
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).
来源:https://stackoverflow.com/questions/49763331/parse-json-to-primative-array-kotlin