引入
implementation 'com.google.code.gson:gson:2.8.6'
方法
//GsonKtx内方法getGson :获取gsontoJson :任意对象转成jsontoAny :将json数据转成任意bean类toMap :将json数据转成MaptoList :将json数据转成任意集合bean类toList2 :将json数据转成任意集合bean类,遇到解析不了的,就使用这个toListType :将json数据转成任意集合bean类,需要自己定义好TypetoListMap :将json数据转成任意集合Map类//扩展方法toJson :任意对象转成jsontoAny :将json数据转成任意bean类toMap :将json数据转成MaptoList :将json数据转成任意集合bean类toList2 :将json数据转成任意集合bean类toListType :将json数据转成任意集合bean类,需要自定义TypetoListMap :将json数据转成任意集合Map类
使用示例
对象转json
val testBean = TestBean(true, "alice")//工具类val json1 = GsonKtx.toJson(testBean)//扩展函数val json2 = testBean.toJson()
json转对象
val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}"val test = GsonKtx.toAny<TestBean>(jsonTest)val test2 = jsonTest.toAny<TestBean>()
json转map
val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}"val map = GsonKtx.toMap<Any>(jsonTest)
json转list
方法一,自己定义好Type,调用toListType
val jsonList = "[{\"isSuccess\":false,\"name\":\"a\"},{\"isSuccess\":true,\"name\":\"b\"}]"val typeToken: TypeToken<List<TestBean>> = object : TypeToken<List<TestBean>>() {}val list = jsonList.toListType<TestBean>(typeToken.type)list?.forEach {println(it.name)}
方法二、调用toList
val list22 = jsonList.toList(Array<TestBean>::class.java)list22?.forEach {println(it.name)}
方法三、调用toList2
val list4 = GsonKtx.toList2<TestBean>(jsonList)println(list4)list4.forEach {println(it.name)}
json转listMap
val list3 = GsonKtx.toListMap<String>(jsonList)println(list3)list3?.forEach {println(it?.get("name"))}
完整代码
/*** 任意对象转成json*/fun Any?.toJson() = GsonKtx.toJson(this)fun Any?.toJson(gson: Gson) = GsonKtx.toJson(gson, this)/***将json数据转成任意bean类*/inline fun <reified T> String?.toAny() = 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)/***将json数据转成Map*/inline fun <reified T> String?.toMap() = GsonKtx.toMap<T>(this)fun <T> String?.toMap(clz: Class<T>?) = GsonKtx.toMap(this, clz)/***将json数据转成任意集合bean类*/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)/***将json数据转成任意集合bean类*/inline fun <reified T> String?.toList2() = GsonKtx.toList2<T>(this)fun <T> String?.toList2(clz: Class<T>?) = GsonKtx.toList2(this, clz)/***将json数据转成任意集合bean类,需要自定义Type*/fun <T> String?.toListType(type: Type) = GsonKtx.toListType<T>(this, type)/***将json数据转成任意集合Map类*/inline fun <reified T> String?.toListMap() = GsonKtx.toListMap<T>(this)fun <T> String?.toListMap(clazz: Class<T>) = GsonKtx.toListMap(this, clazz)object GsonKtx {private val gson = Gson()/***获取 gson*/fun getGson() = gson/*** 任意对象转成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}}/***将json数据转成任意bean类*/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}}/***将json数据转成任意集合bean类*/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}}/***将json数据转成任意集合bean类,需要自己定义好Type* val typeToken: TypeToken<List<TestBean>> = object : TypeToken<List<TestBean>>() {}*/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转List集合,遇到解析不了的,就使用这个*/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).asJsonArrayfor (elem in array) {mList.add(gson.fromJson(elem, cls))}} catch (e: Exception) {}return mList}/*** Json转换成Map的List集合对象*/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>?>?>() {}.typegson.fromJson(json, type)} catch (e: Exception) {null}}/*** Json转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}}}
