Exploring kotin possibilities at first time and a bit confused how to convert JSON string into List object.
data.json is quite simple
{
"0" : "some picture",
"1" : "other picture"
}
Trying to convert like this:
val inputStream: InputStream = context?.assets?.open("data.json") ?: return null
val size: Int = inputStream.available()
val buffer = ByteArray(size)
inputStream.read(buffer)
val jsonString = String(buffer)
println(jsonString)
val gson = Gson()
// THIS LINE CALLS ERROR
val list: List<String> = gson.fromJson(jsonString, Array<String>::class.java).asList()
list.forEachIndexed {index, item -> Log.i("item", "$index::$item")}
And getting error as result
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
How to fix this or how to correctly retrieve list object from json string?