The introduction of
Implementation 'com. Google. Code. Gson: gson: 2.8.6'Copy the code
methods
//GsonKtx inner method getGson: ToAny: convert json data toAny bean class toMap: convert json data toMap toList: convert json data toAny collection bean class toList2 ToListType: Convert JSON data to an arbitrary collection bean class. ToJson: Any object into JSON. ToAny: Json data into any bean class. ToMap: Json data into Map toList ToListType: To convert JSON data into an arbitrary collection bean class, you need to define Type toListMap: to convert JSON data into an arbitrary collection Map classCopy the code
Use the sample
-
Objects to json
Val testBean = testBean (true, "Alice ") // Utility class val json1 = GsonKtx. ToJson (testBean) // Extension function val json2 = testBean.tojson ()Copy the code
-
Turn the json object
val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}" val test = GsonKtx.toAny<TestBean>(jsonTest) val test2 = jsonTest.toAny<TestBean>() Copy the code
-
Json to turn the map
val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}" val map = GsonKtx.toMap<Any>(jsonTest) Copy the code
-
Turn json list
-
Method 1: Define Type and call toListType
val jsonList = "[{\"isSuccess\":false,\"name\":\"a\"},{\"isSuccess\":true,\"name\":\"b\"}]" val typeToken: TypeToken<List<TestBean>> = object : TypeToken<List<TestBean>>() {} vallist = jsonList.toListType<TestBean>(typeToken.type) list? .forEach { println(it.name) }Copy the code
-
Method 2. Call toList
val list22 = jsonList.toList(Array<TestBean>::class.java) list22? .forEach { println(it.name) }Copy the code
-
Method 3. Call toList2
val list4 = GsonKtx.toList2<TestBean>(jsonList) println(list4) list4.forEach { println(it.name) } Copy the code
-
-
Turn json listMap
val list3 = GsonKtx.toListMap<String>(jsonList) println(list3) list3? .forEach { println(it? .get("name")) }Copy the code
The complete code
/** * Convert any object to json */
funAny? .toJson(a) = GsonKtx.toJson(this)
funAny? .toJson(gson: Gson) = GsonKtx.toJson(gson, this)
/** * Convert json data to any bean class */
inline fun <reified T>String? .toAny(a) = GsonKtx.toAny<T>(this)
fun <T>String? .toAny(clazz: Class<T>) = GsonKtx.toAny(this, clazz)
fun <T>String? .toAny(gson: Gson, clazz: Class<T>) = GsonKtx.toAny(gson, this, clazz)
/** * Convert json data to Map */
inline fun <reified T>String? .toMap(a) = GsonKtx.toMap<T>(this)
fun <T>String? .toMap(clz: Class<T>? = GsonKtx.toMap(this, clz)
/** * Convert json data to any collection bean class */
fun <T>String? .toList(clz: Class<Array<T> >? = GsonKtx.toList(this, clz)
fun <T>String? .toList(gson: Gson, clz: Class<Array<T> >? = GsonKtx.toList(gson, this, clz)
/** * Convert json data to any collection bean class */
inline fun <reified T>String? .toList2(a) = GsonKtx.toList2<T>(this)
fun <T>String? .toList2(clz: Class<T>? = GsonKtx.toList2(this, clz)
/** * To convert JSON data into any collection bean class, you need to customize Type */
fun <T>String? .toListType(type: Type) = GsonKtx.toListType<T>(this, type)
/** * Convert JSON data to any collection Map class */
inline fun <reified T>String? .toListMap(a) = GsonKtx.toListMap<T>(this)
fun <T>String? .toListMap(clazz: Class<T>) = GsonKtx.toListMap(this, clazz)
object GsonKtx {
private val gson = Gson()
/** * get gson */
fun getGson(a) = gson
/** * Convert any object to json */
fun toJson(any: Any?).: String? {
return toJson(gson, any)
}
fun toJson(gson: Gson, any: Any?).: String? {
return try {
gson.toJson(any)
} catch (e: Exception) {
null}}/** * Convert json data to any bean class */
inline fun <reified T> toAny(json: String?).: T? {
return toAny(json, T::class.java)
}
fun <T> toAny(json: String? , clazz:Class<T>): T? {
return toAny(gson, json, clazz)
}
fun <T> toAny(gson: Gson, json: String? , clazz:Class<T>): T? {
return try {
gson.fromJson(json, clazz)
} catch (e: Exception) {
null}}/** * Convert json data to any collection bean class */
fun <T> toList(json: String? , clz:Class<Array<T> >?: List<T>? {
return toList(gson, json, clz)
}
fun <T> toList(gson: Gson, json: String? , clz:Class<Array<T> >?: List<T>? {
return try {
val ts: Array<T> = gson.fromJson(json, clz)
mutableListOf(*ts)
} catch (e: Exception) {
null}}Type * val typeToken: typeToken
> = object: TypeToken
>() {} */
fun <T> toListType(json: String? , type:Type): List<T>? {
return toListType(gson, json, type)
}
fun <T> toListType(gson: Gson, json: String? , type:Type): List<T>? {
return try {
gson.fromJson(json, type)
} catch (e: Exception) {
null}}/** ** Json to List, if you can't parse it, use */
inline fun <reified T> toList2(json: String?).: List<T> {
return toList2(json, T::class.java)
}
fun <T> toList2(json: String? , cls:Class<T>?: List<T> {
val mList: MutableList<T> = ArrayList()
try {
val array = JsonParser().parse(json).asJsonArray
for (elem in array) {
mList.add(gson.fromJson(elem, cls))
}
} catch (e: Exception) {
}
return mList
}
/** * Json to Map List objects */
inline fun <reified T> toListMap(json: String?).: List<Map<String? , T>? >? {return toListMap(json, T::class.java)
}
fun <T> toListMap(json: String? , clz:Class<T>?: List<Map<String? , T>? >? {return toListMap(gson, json, clz)
}
fun <T> toListMap(gson: Gson, json: String? , clz:Class<T>?: List<Map<String? , T>? >? {return try {
val type: Type = object: TypeToken<List<Map<String? , T>? >? >() {}.type gson.fromJson(json, type) }catch (e: Exception) {
null}}/** * Json to Map */
inline fun <reified T> toMap(json: String?).: Map<String? , T>? {return toMap(json, T::class.java)
}
fun <T> toMap(json: String? , clz:Class<T>?: Map<String? , T>? {return toMap(gson, json, clz)
}
fun <T> toMap(gson: Gson, json: String? , clazz:Class<T>?: Map<String? , T>? {return try {
val type = object: TypeToken<Map<String? , T>? >() {}.typereturn gson.fromJson(json, type)
} catch (e: Exception) {
null}}}Copy the code