Android:使用Gson常见问题(包含解决将Long型转化为科学计数法的问题)

一、解决将Long型转化为科学计数法的问题

1.1 场景

将一个对象转为Map类型时,调用Gson.fromJson发现,原来对象中的long类型的personId字段,被解析成了科学计数法,导致请求接口失败,报参数错误。

解决结果图
在这里插入图片描述

1.2、ExampleUnitTest.kt示例代码
data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
    private val mGson: Gson = Gson()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson: String = mGson.toJson(reuestParmObj)
        println("reuestParmJson信息:$reuestParmJson")
        val mapParm: Map<String, *> =
            mGson.fromJson(reuestParmJson, Map::class.java) as Map<String, *>
        println("mapParms信息:$mapParm")
    }
}

测试结果
在这里插入图片描述

1.3 解决将Long型转化为科学计数法问题的方案

修改gson配置
private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create()

data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
//    private val mGson: Gson = Gson()
  private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson: String = mGson.toJson(reuestParmObj)
        println("reuestParmJson信息:$reuestParmJson")
        val mapParm: Map<String, *> =
            mGson.fromJson(reuestParmJson, Map::class.java) as Map<String, *>
        println("mapParms信息:$mapParm")
    }
}

测试结果
在这里插入图片描述

二、 把null值也转换,默认是不转换null值的

设置:serializeNulls()

 private val mGson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls().create()

三、Html的转换

Gson默认是把Html 转义的,但也可以设置不转义。
设置:disableHtmlEscaping()

class ExampleUnitTest {
	@Test
    fun testDisableHtmlEscaping(){
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().disableHtmlEscaping().create()
        val htmlStr = "<h3>测试disableHtmlEscaping</h3>"
        println("未设置disableHtmlEscaping转换htmlStr结果:${gson1.toJson(htmlStr)}")
        println("设置disableHtmlEscaping转换htmlStr结果:${gson2.toJson(htmlStr)}")
    }
}

测试结果
在这里插入图片描述

四、设置输入json格式

默认的输出是compact格式,示例
{"personId":1668394335647,"personType":1}
设置:setPrettyPrinting()

class ExampleUnitTest {
	 @Test
    fun testSetPrettyPrinting(){
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).setPrettyPrinting().create()
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        println("未设置 setPrettyPrinting 转换reuestParmObj结果:${gson1.toJson(reuestParmObj)}")
        println("设置 setPrettyPrinting 转换reuestParmObj结果:${gson2.toJson(reuestParmObj)}")
    }
}

测试结果
在这里插入图片描述

五、ExampleUnitTest.kt文件完整代码

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.LongSerializationPolicy
import com.google.gson.reflect.TypeToken
import org.junit.Test

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */

data class ReuestParmObj(val personId: Long = 1668394335647, val personType: Int = 1)
class ExampleUnitTest {
    private val mGson1: Gson = Gson()
    private val mGson2: Gson =
        GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls()
            .disableHtmlEscaping().create()

    @Test
    fun gsonReuestParmJson() {
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        val reuestParmJson1: String = mGson1.toJson(reuestParmObj)
        val reuestParmJson2: String = mGson2.toJson(reuestParmObj)
        println("reuestParmJson1 信息:$reuestParmJson1")
        println("reuestParmJson2 信息:$reuestParmJson2")

        //        val type = object : TypeToken<Map<String, *>>() {}.type
//        val mapParm: Map<String, *> = mGson1.fromJson(reuestParmJson, type)

        val mapParm1: Map<String, *> =
            mGson1.fromJson(reuestParmJson1, Map::class.java) as Map<String, *>
        println("mapParm1 信息:$mapParm1")
        val mapParm2: Map<String, *> =
            mGson2.fromJson(reuestParmJson2, Map::class.java) as Map<String, *>
        println("mapParms2 信息:$mapParm2")
        /*
            reuestParmJson1 信息:{"personId":1668394335647,"personType":1}
            reuestParmJson2 信息:{"personId":"1668394335647","personType":1}
            mapParm1 信息:{personId=1.668394335647E12, personType=1.0}
            mapParms2 信息:{personId=1668394335647, personType=1.0}
         */

    }

    @Test
    fun testDisableHtmlEscaping() {
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().disableHtmlEscaping().create()
        val htmlStr = "<h3>测试disableHtmlEscaping</h3>"
        println("未设置disableHtmlEscaping转换htmlStr结果:${gson1.toJson(htmlStr)}")
        println("设置disableHtmlEscaping转换htmlStr结果:${gson2.toJson(htmlStr)}")
        /*
            未设置disableHtmlEscaping转换htmlStr结果:"\u003ch3\u003e测试disableHtmlEscaping\u003c/h3\u003e"
            设置disableHtmlEscaping转换htmlStr结果:"<h3>测试disableHtmlEscaping</h3>"
         */
    }

    @Test
    fun testSetPrettyPrinting() {
        val gson1: Gson = Gson()
        val gson2: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING)
            .setPrettyPrinting().create()
        val reuestParmObj = ReuestParmObj(1668394335647, 1)
        println("未设置 setPrettyPrinting 转换reuestParmObj结果:${gson1.toJson(reuestParmObj)}")
        println("设置 setPrettyPrinting 转换reuestParmObj结果:${gson2.toJson(reuestParmObj)}")
        /*
           未设置 setPrettyPrinting 转换reuestParmObj结果:{"personId":1668394335647,"personType":1}
           设置 setPrettyPrinting 转换reuestParmObj结果:{
             "personId": "1668394335647",
             "personType": 1
           }
        */
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值