Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert string array JSON to be ArrayList in Kotlin?

so I have this JSON in string format:

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

but I need to convert it to ArrayList In Kotlin ? I have tried to find, but most of the answer are in Java. if using gson in Java maybe it will be like this. but I am a beginner and I failed to convert the code below to 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 ?

like image 254
Agung Laksana Avatar asked Aug 26 '26 18:08

Agung Laksana


1 Answers

Maybe this is the kotlin code you want, converted from your Java code:

val gson = GsonBuilder().create()
val theList = gson.fromJson<ArrayList<String>>(stringObject, object :TypeToken<ArrayList<String>>(){}.type)
like image 138
L. Swifter Avatar answered Aug 29 '26 10:08

L. Swifter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!