将Json解析为基本数组Kotlin [英] Parse Json to Primative Array Kotlin
本文介绍了将Json解析为基本数组Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
也许这很简单,但是我却不知道该怎么做.我正在使用GSON,kotlin和改造
Data.json
{"array1":[1,2],"array2":[1,2]}
DataObject.kt
数据类DataObject(array1:List< Int> ;, array2:List< Int>)
以上内容未能反序列化数组.
解决方案
这是一个正在运行的示例:
数据类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])}
我仅将 DataObject
的参数更改为 val
,这对于数据类是必需的( var
也可以)./p>
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.
解决方案
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).
这篇关于将Json解析为基本数组Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文