I have a problem with Kotlin writing code to convert from json String to a list of objects.
Normal in Java is something like this:
Gson gson = new Gson();
Type type = new TypeToken<List<SomeOjbect>>() {}.getType();
List<SomeOjbect> measurements = gson.fromJson(json, type);
return measurements;
However, in Kotlin, when I try it like this:
val gson = Gson()
val type: Type = TypeToken<List<SomeOjbect>>{}.type
val measurements : List<SomeOjbect> = gson.fromJson(text, type)
return measurements
Android Studio IDE underlines as TypeToken error:
Unable to access <init> ': this is public / package / in' TypeToken '
and also underlines as an error {}:
Type mismatch. Necessary: Type! Found: () → Unit
So, is there a solution to make it work for Kotlin?
source
share