引入

  1. implementation 'com.google.code.gson:gson:2.8.6'

方法

  1. //GsonKtx内方法
  2. getGson :获取gson
  3. toJson :任意对象转成json
  4. toAny :将json数据转成任意bean
  5. toMap :将json数据转成Map
  6. toList :将json数据转成任意集合bean
  7. toList2 :将json数据转成任意集合bean类,遇到解析不了的,就使用这个
  8. toListType :将json数据转成任意集合bean类,需要自己定义好Type
  9. toListMap :将json数据转成任意集合Map
  10. //扩展方法
  11. toJson :任意对象转成json
  12. toAny :将json数据转成任意bean
  13. toMap :将json数据转成Map
  14. toList :将json数据转成任意集合bean
  15. toList2 :将json数据转成任意集合bean
  16. toListType :将json数据转成任意集合bean类,需要自定义Type
  17. toListMap :将json数据转成任意集合Map

使用示例

  • 对象转json

    1. val testBean = TestBean(true, "alice")
    2. //工具类
    3. val json1 = GsonKtx.toJson(testBean)
    4. //扩展函数
    5. val json2 = testBean.toJson()
  • json转对象

    1. val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}"
    2. val test = GsonKtx.toAny<TestBean>(jsonTest)
    3. val test2 = jsonTest.toAny<TestBean>()
  • json转map

    1. val jsonTest = "{\"isSuccess\":\"true\",\"name\":alice}"
    2. val map = GsonKtx.toMap<Any>(jsonTest)
  • json转list

    • 方法一,自己定义好Type,调用toListType

      1. val jsonList = "[{\"isSuccess\":false,\"name\":\"a\"},{\"isSuccess\":true,\"name\":\"b\"}]"
      2. val typeToken: TypeToken<List<TestBean>> = object : TypeToken<List<TestBean>>() {}
      3. val list = jsonList.toListType<TestBean>(typeToken.type)
      4. list?.forEach {
      5. println(it.name)
      6. }
    • 方法二、调用toList

      1. val list22 = jsonList.toList(Array<TestBean>::class.java)
      2. list22?.forEach {
      3. println(it.name)
      4. }
    • 方法三、调用toList2

      1. val list4 = GsonKtx.toList2<TestBean>(jsonList)
      2. println(list4)
      3. list4.forEach {
      4. println(it.name)
      5. }
  • json转listMap

    1. val list3 = GsonKtx.toListMap<String>(jsonList)
    2. println(list3)
    3. list3?.forEach {
    4. println(it?.get("name"))
    5. }

完整代码

  1. /**
  2. * 任意对象转成json
  3. */
  4. fun Any?.toJson() = GsonKtx.toJson(this)
  5. fun Any?.toJson(gson: Gson) = GsonKtx.toJson(gson, this)
  6. /**
  7. *将json数据转成任意bean类
  8. */
  9. inline fun <reified T> String?.toAny() = GsonKtx.toAny<T>(this)
  10. fun <T> String?.toAny(clazz: Class<T>) = GsonKtx.toAny(this, clazz)
  11. fun <T> String?.toAny(gson: Gson, clazz: Class<T>) = GsonKtx.toAny(gson, this, clazz)
  12. /**
  13. *将json数据转成Map
  14. */
  15. inline fun <reified T> String?.toMap() = GsonKtx.toMap<T>(this)
  16. fun <T> String?.toMap(clz: Class<T>?) = GsonKtx.toMap(this, clz)
  17. /**
  18. *将json数据转成任意集合bean类
  19. */
  20. fun <T> String?.toList(clz: Class<Array<T>>?) = GsonKtx.toList(this, clz)
  21. fun <T> String?.toList(gson: Gson, clz: Class<Array<T>>?) = GsonKtx.toList(gson, this, clz)
  22. /**
  23. *将json数据转成任意集合bean类
  24. */
  25. inline fun <reified T> String?.toList2() = GsonKtx.toList2<T>(this)
  26. fun <T> String?.toList2(clz: Class<T>?) = GsonKtx.toList2(this, clz)
  27. /**
  28. *将json数据转成任意集合bean类,需要自定义Type
  29. */
  30. fun <T> String?.toListType(type: Type) = GsonKtx.toListType<T>(this, type)
  31. /**
  32. *将json数据转成任意集合Map类
  33. */
  34. inline fun <reified T> String?.toListMap() = GsonKtx.toListMap<T>(this)
  35. fun <T> String?.toListMap(clazz: Class<T>) = GsonKtx.toListMap(this, clazz)
  36. object GsonKtx {
  37. private val gson = Gson()
  38. /**
  39. *获取 gson
  40. */
  41. fun getGson() = gson
  42. /**
  43. * 任意对象转成json
  44. */
  45. fun toJson(any: Any?): String? {
  46. return toJson(gson, any)
  47. }
  48. fun toJson(gson: Gson, any: Any?): String? {
  49. return try {
  50. gson.toJson(any)
  51. } catch (e: Exception) {
  52. null
  53. }
  54. }
  55. /**
  56. *将json数据转成任意bean类
  57. */
  58. inline fun <reified T> toAny(json: String?): T? {
  59. return toAny(json, T::class.java)
  60. }
  61. fun <T> toAny(json: String?, clazz: Class<T>): T? {
  62. return toAny(gson, json, clazz)
  63. }
  64. fun <T> toAny(gson: Gson, json: String?, clazz: Class<T>): T? {
  65. return try {
  66. gson.fromJson(json, clazz)
  67. } catch (e: Exception) {
  68. null
  69. }
  70. }
  71. /**
  72. *将json数据转成任意集合bean类
  73. */
  74. fun <T> toList(json: String?, clz: Class<Array<T>>?): List<T>? {
  75. return toList(gson, json, clz)
  76. }
  77. fun <T> toList(gson: Gson, json: String?, clz: Class<Array<T>>?): List<T>? {
  78. return try {
  79. val ts: Array<T> = gson.fromJson(json, clz)
  80. mutableListOf(*ts)
  81. } catch (e: Exception) {
  82. null
  83. }
  84. }
  85. /**
  86. *将json数据转成任意集合bean类,需要自己定义好Type
  87. * val typeToken: TypeToken<List<TestBean>> = object : TypeToken<List<TestBean>>() {}
  88. */
  89. fun <T> toListType(json: String?, type: Type): List<T>? {
  90. return toListType(gson, json, type)
  91. }
  92. fun <T> toListType(gson: Gson, json: String?, type: Type): List<T>? {
  93. return try {
  94. gson.fromJson(json, type)
  95. } catch (e: Exception) {
  96. null
  97. }
  98. }
  99. /**
  100. * Json转List集合,遇到解析不了的,就使用这个
  101. */
  102. inline fun <reified T> toList2(json: String?): List<T> {
  103. return toList2(json, T::class.java)
  104. }
  105. fun <T> toList2(json: String?, cls: Class<T>?): List<T> {
  106. val mList: MutableList<T> = ArrayList()
  107. try {
  108. val array = JsonParser().parse(json).asJsonArray
  109. for (elem in array) {
  110. mList.add(gson.fromJson(elem, cls))
  111. }
  112. } catch (e: Exception) {
  113. }
  114. return mList
  115. }
  116. /**
  117. * Json转换成Map的List集合对象
  118. */
  119. inline fun <reified T> toListMap(json: String?): List<Map<String?, T>?>? {
  120. return toListMap(json, T::class.java)
  121. }
  122. fun <T> toListMap(json: String?, clz: Class<T>?): List<Map<String?, T>?>? {
  123. return toListMap(gson, json, clz)
  124. }
  125. fun <T> toListMap(gson: Gson, json: String?, clz: Class<T>?): List<Map<String?, T>?>? {
  126. return try {
  127. val type: Type = object : TypeToken<List<Map<String?, T>?>?>() {}.type
  128. gson.fromJson(json, type)
  129. } catch (e: Exception) {
  130. null
  131. }
  132. }
  133. /**
  134. * Json转Map对象
  135. */
  136. inline fun <reified T> toMap(json: String?): Map<String?, T>? {
  137. return toMap(json, T::class.java)
  138. }
  139. fun <T> toMap(json: String?, clz: Class<T>?): Map<String?, T>? {
  140. return toMap(gson, json, clz)
  141. }
  142. fun <T> toMap(gson: Gson, json: String?, clazz: Class<T>?): Map<String?, T>? {
  143. return try {
  144. val type = object : TypeToken<Map<String?, T>?>() {}.type
  145. return gson.fromJson(json, type)
  146. } catch (e: Exception) {
  147. null
  148. }
  149. }
  150. }