简体   繁体   English

如何在 Kotlin 中将字符串数组 JSON 转换为 ArrayList?

[英]how to convert string array JSON to be ArrayList in Kotlin?

so I have this JSON in string format:所以我有这个字符串格式的JSON:

[
  "1.0",
  "1.1",
  "1.2"
]

but I need to convert it to ArrayList In Kotlin ?但我需要在 Kotlin 中将其转换为 ArrayList 吗? I have tried to find, but most of the answer are in Java.我试图找到,但大部分答案都在 Java 中。 if using gson in Java maybe it will be like this.如果在 Java 中使用 gson 可能会是这样。 but I am a beginner and I failed to convert the code below to Kotlin但我是初学者,我未能将下面的代码转换为 Kotlin

Gson gson = new GsonBuilder().create();
ArrayList<String> theList = gson.fromJson(stringObject, new TypeToken<ArrayList<String>>(){}.getType())

what should I do to convert that json string to array list in Kotlin ?我应该怎么做才能将该 json 字符串转换为 Kotlin 中的数组列表?

Maybe this is the kotlin code you want, converted from your Java code:也许这是你想要的 kotlin 代码,从你的 Java 代码转换而来:

val gson = GsonBuilder().create()
val theList = gson.fromJson<ArrayList<String>>(stringObject, object :TypeToken<ArrayList<String>>(){}.type)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM