Skip to content Skip to sidebar Skip to footer

Parse Json To Primative Array Kotlin

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(arr

Solution 1:

This is a running example:

dataclassDataObject(val array1: List<Int>, val array2: List<Int>)

funmain(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).

Post a Comment for "Parse Json To Primative Array Kotlin"