版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/YANGWEIQIAO/article/details/78977927
在java中,我们是这样使用的:
Type type = new TypeToken<Result>() {
}.getType();
然后使用Gson去解析对象:
gson.fromJson(json, type);
在Kotlin中:
第一种方式:
val turnsType = object : TypeToken<List<Turns>>() {}.type
val turns = Gson().fromJson<List<Turns>>(pref.turns, turnsType) //解析
第二种方式:更方便
inline fun <reified T> genericType() = object: TypeToken<T>() {}.type
val turnsType = genericType<List<Turns>>()