Im stuck at what probably has a simple solution.
I have a string representation of a list, like this:
"[[1, 2, 3], [4, 5, 6]]"
In other words, a list containing 2 lists of 3 integers
How to convert the string to a list object of List<List> in Kotlin?
You can use kotlinx.serialization
to deserialize JSON!
As a standalone Kotlin script:
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0")
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
val s = "[[1, 2, 3], [4, 5, 6]]"
val j = Json.decodeFromString<List<List<Int>>>(s)
println(j) // [[1, 2, 3], [4, 5, 6]]
println(j[0][0]) // 1
In an Android app's build.gradle
you would need these lines instead of @file:DependsOn
:
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0'
}
apply plugin: 'kotlinx-serialization'