一、解决将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



&spm=1001.2101.3001.5002&articleId=141300632&d=1&t=3&u=2a4ae9c34b8f495db6155504c697f8b3)
2774

被折叠的 条评论
为什么被折叠?



