Json解析工具封装

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

二、Fastjson

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

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

你可能感兴趣的:(Json解析工具封装)