将凌空字符串响应(List< List< Int>>)转换为Kotlin列表 [英] Convert volley string response (List<List<Int>>) to Kotlin list

查看:74
本文介绍了将凌空字符串响应(List< List< Int>>)转换为Kotlin列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在可能有一个简单解决方案的地方.

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]]"

换句话说,一个包含2个包含3个整数的列表的列表

In other words, a list containing 2 lists of 3 integers

如何将字符串转换为List< List>的列表对象在科特林吗?

How to convert the string to a list object of List<List> in Kotlin?

推荐答案

您可以使用 kotlinx.serialization 以反序列化JSON!

You can use kotlinx.serialization to deserialize JSON!

作为独立的Kotlin脚本:

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

在Android应用程序的 build.gradle 中,您需要这些行,而不是 @file:DependsOn :

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'

这篇关于将凌空字符串响应(List&lt; List&lt; Int&gt;&gt;)转换为Kotlin列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆