我有一个问题,Kotlin编写从json String转换为对象List的代码.
Java中的Normaly就像这样:
Gson gson = new Gson();
Type type = new TypeToken>() {}.getType();
List measurements = gson.fromJson(json, type);
return measurements;
但是在Kotlin,当我尝试这样的时候:
val gson = Gson()
val type: Type = TypeToken>{}.type
val measurements : List = gson.fromJson(text, type)
return measurements
IDE Android Studio强调了TypeToken所说的错误:
Cannot access ‘ < init > ‘: it is public/package/ in ‘TypeToken’
并且还强调错误{}说:
Type mismatch.
Required:
Type!
Found:
() → Unit
那么有没有一个解决方案让它适用于Kotlin?