Parse Json to Primative Array Kotlin

六月ゝ 毕业季﹏ 提交于 2021-01-28 12:32:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!