Json解析工具封装

2021-05-23  本文已影响0人  FlyClound

部分内容有错误,可以去参考其他几个,Gson解析可参考这篇文章,错误内容com.google.gson.internal.LikedTreeMap connot be case to class可参考这篇,fastjson解析没有太大问题,更详细FastJson封装可以参考这篇。

一、Gson

导包:implementation 'com.google.code.gson:gson:2.8.6'

/**
 * 任意对象转成json
 */
fun Any?.toJson() = GsonUtils.any2Json(this)
fun Any?.toJson(gson: Gson) = GsonUtils.any2Json(gson, this)
/**
 *将json数据转成任意bean类
 */
fun <T> String?.toAny(clazz: Class<T>) = GsonUtils.json2Any(this, clazz)
fun <T> String?.toAny(gson: Gson, clazz: Class<T>) = GsonUtils.json2Any(gson, this, clazz)
fun <T> String?.toMap(clz: Class<T>?) = GsonUtils.json2Map(this,clz)
/**
 *将json数据转成任意集合bean类
 */
fun <T> String?.toList(clazz: Class<T>) = GsonUtils.json2List(this, clazz)
fun <T> String?.toList(gson: Gson, clazz: Class<T>) = GsonUtils.json2List(gson, this, clazz)
fun <T> String?.toListMap(clazz: Class<T>) = GsonUtils.json2ListMap(this, clazz)
object GsonUtils {
    private val gson = Gson()
    /**
     *获取 gson
     */
    fun getGson() = gson
    /**
     *将json数据转成任意bean类
     */
    fun <T> json2Any(json: String?, clazz: Class<T>): T? {
        return json2Any(gson, json, clazz)
    }
    fun <T> json2Any(gson: Gson, json: String?, clazz: Class<T>): T? {
        return try {
            gson.fromJson(json, clazz)
        } catch (e: Exception) {
            null
        }
    }
    /**
     * 任意对象转成json
     */
    fun any2Json(any: Any?): String? {
        return any2Json(gson, any)
    }
    fun any2Json(gson: Gson, any: Any?): String? {
        return try {
            gson.toJson(any)
        } catch (e: Exception) {
            null
        }
    }
    /**
     *将json数据转成任意集合bean类
     */
    fun <T> json2List(json: String?, clazz: Class<T>): List<T>? {
        val typeToken: TypeToken<List<T>> = object : TypeToken<List<T>>() {}
        return json2List(gson, json, clazz)
    }
    fun <T> json2List(gson: Gson, json: String?, clazz: Class<T>): List<T>? {
        val typeToken: TypeToken<List<T>> = object : TypeToken<List<T>>() {}
        return try {
            gson.fromJson(json, typeToken.type)
        } catch (e: Exception) {
            null
        }
    }
    /**
     * Json转List集合,遇到解析不了的,就使用这个
     */
    fun <T> json2List2(json: String?, cls: Class<T>?): List<T>? {
        val mList: MutableList<T> = ArrayList()
        val array = JsonParser().parse(json).asJsonArray
        for (elem in array) {
            mList.add(gson.fromJson(elem, cls))
        }
        return mList
    }
    /**
     * Json转换成Map的List集合对象
     */
    fun <T> json2ListMap(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转Map对象
     */
    fun <T> json2Map(json: String?, clz: Class<T>?): Map<String?, T>? {
        val type = object : TypeToken<Map<String?, T>?>() {}.type
        return gson.fromJson(json, type)
    }
}

二、Fastjson

导包: implementation 'com.alibaba:fastjson:1.2.48'

//json -> any
fun <T>String?.toAny(tClass: Class<T>?) = FastUtils.json2Class(this,tClass)
//json -> map
fun <T>String?.toMap() = FastUtils.json2Map<Map<String,T>>(this)
//json -> list
fun <T>String?.toList(tClass: Class<T>?) = FastUtils.json2List(this,tClass)
//any -> json
fun Any?.toJsonFast() = FastUtils.any2Json(this)
object FastUtils {
    /**
     *json  ----->  T
     * @param T
     * @param json
     * @param tClass
     * @return
     */
    fun <T> json2Class(json: String?, tClass: Class<T>?): T? {
        if (TextUtils.isEmpty(json)) {
            return null
        }
        try {
            return JSON.parseObject(json, tClass)
        } catch (e: Exception) {
        }
        return null
    }
    /**
     *json ---->  map
     * @param T
     * @param json
     * @return
     */
    fun <T>json2Map(json: String?): Map<String, T>? {
        if (TextUtils.isEmpty(json)) {
            return null
        }
        try {
            return JSON.parseObject<Map<String, T>>(json, MutableMap::class.java)
        } catch (e: Exception) {
        }
        return null
    }
    /**
     *json ----->  list
     * @param T
     * @param json
     * @param tClass
     * @return
     */
    fun <T> json2List(json: String?, tClass: Class<T>?): List<T>? {
        if (TextUtils.isEmpty(json)) {
            return null
        }
        try {
            return JSON.parseArray(json, tClass)
        } catch (e: Exception) {
        }
        return null
    }
    /**
     *any -----> json
     * @param any
     * @return
     */
    fun any2Json(any: Any?): String?{
        return try {
            JSON.toJSONString(any)
        }catch (e: Exception){
            null
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读