一、Gson

导包:implementation ‘com.google.code.gson:gson:2.8.6’

  1. /**
  2. * 任意对象转成json
  3. */
  4. fun Any?.toJson() = GsonUtils.any2Json(this)
  5. fun Any?.toJson(gson: Gson) = GsonUtils.any2Json(gson, this)
  6. /**
  7. *将json数据转成任意bean类
  8. */
  9. fun <T> String?.toAny(clazz: Class<T>) = GsonUtils.json2Any(this, clazz)
  10. fun <T> String?.toAny(gson: Gson, clazz: Class<T>) = GsonUtils.json2Any(gson, this, clazz)
  11. fun <T> String?.toMap(clz: Class<T>?) = GsonUtils.json2Map(this,clz)
  12. /**
  13. *将json数据转成任意集合bean类
  14. */
  15. fun <T> String?.toList(clazz: Class<T>) = GsonUtils.json2List(this, clazz)
  16. fun <T> String?.toList(gson: Gson, clazz: Class<T>) = GsonUtils.json2List(gson, this, clazz)
  17. fun <T> String?.toListMap(clazz: Class<T>) = GsonUtils.json2ListMap(this, clazz)
  18. object GsonUtils {
  19. private val gson = Gson()
  20. /**
  21. *获取 gson
  22. */
  23. fun getGson() = gson
  24. /**
  25. *将json数据转成任意bean类
  26. */
  27. fun <T> json2Any(json: String?, clazz: Class<T>): T? {
  28. return json2Any(gson, json, clazz)
  29. }
  30. fun <T> json2Any(gson: Gson, json: String?, clazz: Class<T>): T? {
  31. return try {
  32. gson.fromJson(json, clazz)
  33. } catch (e: Exception) {
  34. null
  35. }
  36. }
  37. /**
  38. * 任意对象转成json
  39. */
  40. fun any2Json(any: Any?): String? {
  41. return any2Json(gson, any)
  42. }
  43. fun any2Json(gson: Gson, any: Any?): String? {
  44. return try {
  45. gson.toJson(any)
  46. } catch (e: Exception) {
  47. null
  48. }
  49. }
  50. /**
  51. *将json数据转成任意集合bean类
  52. */
  53. fun <T> json2List(json: String?, clazz: Class<T>): List<T>? {
  54. val typeToken: TypeToken<List<T>> = object : TypeToken<List<T>>() {}
  55. return json2List(gson, json, clazz)
  56. }
  57. fun <T> json2List(gson: Gson, json: String?, clazz: Class<T>): List<T>? {
  58. val typeToken: TypeToken<List<T>> = object : TypeToken<List<T>>() {}
  59. return try {
  60. gson.fromJson(json, typeToken.type)
  61. } catch (e: Exception) {
  62. null
  63. }
  64. }
  65. /**
  66. * Json转List集合,遇到解析不了的,就使用这个
  67. */
  68. fun <T> json2List2(json: String?, cls: Class<T>?): List<T>? {
  69. val mList: MutableList<T> = ArrayList()
  70. val array = JsonParser().parse(json).asJsonArray
  71. for (elem in array) {
  72. mList.add(gson.fromJson(elem, cls))
  73. }
  74. return mList
  75. }
  76. /**
  77. * Json转换成Map的List集合对象
  78. */
  79. fun <T> json2ListMap(json: String?, clz: Class<T>?): List<Map<String?, T>?>? {
  80. return try {
  81. val type: Type = object : TypeToken<List<Map<String?, T>?>?>() {}.type
  82. gson.fromJson(json, type)
  83. } catch (e: Exception) {
  84. null
  85. }
  86. }
  87. /**
  88. * Json转Map对象
  89. */
  90. fun <T> json2Map(json: String?, clz: Class<T>?): Map<String?, T>? {
  91. val type = object : TypeToken<Map<String?, T>?>() {}.type
  92. return gson.fromJson(json, type)
  93. }
  94. }

二、Fastjson

implementation ‘com.alibaba:fastjson:1.2.48’

  1. //json -> any
  2. fun <T>String?.toAny(tClass: Class<T>?) = FastUtils.json2Class(this,tClass)
  3. //json -> map
  4. fun <T>String?.toMap() = FastUtils.json2Map<Map<String,T>>(this)
  5. //json -> list
  6. fun <T>String?.toList(tClass: Class<T>?) = FastUtils.json2List(this,tClass)
  7. //any -> json
  8. fun Any?.toJsonFast() = FastUtils.any2Json(this)
  9. object FastUtils {
  10. /**
  11. *json -----> T
  12. * @param T
  13. * @param json
  14. * @param tClass
  15. * @return
  16. */
  17. fun <T> json2Class(json: String?, tClass: Class<T>?): T? {
  18. if (TextUtils.isEmpty(json)) {
  19. return null
  20. }
  21. try {
  22. return JSON.parseObject(json, tClass)
  23. } catch (e: Exception) {
  24. }
  25. return null
  26. }
  27. /**
  28. *json ----> map
  29. * @param T
  30. * @param json
  31. * @return
  32. */
  33. fun <T>json2Map(json: String?): Map<String, T>? {
  34. if (TextUtils.isEmpty(json)) {
  35. return null
  36. }
  37. try {
  38. return JSON.parseObject<Map<String, T>>(json, MutableMap::class.java)
  39. } catch (e: Exception) {
  40. }
  41. return null
  42. }
  43. /**
  44. *json -----> list
  45. * @param T
  46. * @param json
  47. * @param tClass
  48. * @return
  49. */
  50. fun <T> json2List(json: String?, tClass: Class<T>?): List<T>? {
  51. if (TextUtils.isEmpty(json)) {
  52. return null
  53. }
  54. try {
  55. return JSON.parseArray(json, tClass)
  56. } catch (e: Exception) {
  57. }
  58. return null
  59. }
  60. /**
  61. *any -----> json
  62. * @param any
  63. * @return
  64. */
  65. fun any2Json(any: Any?): String?{
  66. return try {
  67. JSON.toJSONString(any)
  68. }catch (e: Exception){
  69. null
  70. }
  71. }
  72. }