Using Gson in Kotlin to parse JSON array
You need to change parameter in your fromJson()
function call like following:
val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()
You need to pass Array<WeatherObject>::class.java
for class type and then convert result into List
. No need to change registerTypeAdapter()
function call.
Check following code:
fun getWeatherObjectFromJson(jsonStr: String): List<WeatherObject> {
var stringReader: StringReader = StringReader(jsonStr)
var jsonReader: JsonReader = JsonReader(stringReader)
val gsonBuilder = GsonBuilder().serializeNulls()
gsonBuilder.registerTypeAdapter(WeatherObject::class.java, WeatherDeserializer())
val gson = gsonBuilder.create()
val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()
return weatherList
}