1.字符串 转成 map
fun String.toMap(): MutableMap<String, Any> {
return Gson().fromJson(this, object : TypeToken<MutableMap<String, Any>>() {}.type)
}
fun String.toToMapStr(): MutableMap<String, String> {
return Gson().fromJson(this, object : TypeToken<MutableMap<String, String>>() {}.type)
}
2.字符串 转成 list
fun String.toToListMap(): MutableList<MutableMap<String, Any>> {
return Gson().fromJson(this, object : TypeToken<MutableMap<String, Any>>() {}.type)
}
inline fun <reified T> String.toList(): MutableList<T> {
return Gson().fromJson(this, object : TypeToken<MutableList<T>>() {}.type)
}
3.字符串 转成 class,泛型
inline fun <reified T> String.toBean(): T {
return Gson().fromJson(this, object : TypeToken<T>() {}.type)
}
4. Class 转成 字符串
fun Any?.toJson(): String {
if (this == null) return ""
if (this is String) return this.toString()
return Gson().toJson(this)
}