解决GSON转Long型变为科学计数法的问题
程序员文章站
2022-03-13 12:53:17
...
一、依赖包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
二、代码部分
public static void main(String[] args)
{
String jsonStr = "{\"ret_code\":\"0\",\"ret_msg\":\"成功\",\"serial_number\":\"20181227101254HQFAW236359\",\"timestamp\":\"2018-12-27 02:13:01\",\"response_data\":{\"token\":\"196ad86b\",\"expiredMills\":1546481581194}}";
RnrResponseVO<ResObtainTokenVO> resObtainTokenVORnrResponseVO = (RnrResponseVO<ResObtainTokenVO>) jsonToObjectLong(jsonStr,
RnrResponseVO.builder().response_data(ResObtainTokenVO.class).build().getClass());
log.info("resObtainTokenVORnrResponseVO={}", resObtainTokenVORnrResponseVO);
}
/**
* json to VO实例 并且处理对应的科学计数法
*
* @param <T>
* @param obj
* @return
* @exception
* @date 2018/12/27 9:49
*/
public static <T> Object jsonToObjectLong(String json, Class<T> obj)
{
return new Gson().fromJson(
new GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).create()
.toJson(JSONObject.parse(json)), obj);
}
三、转换后的结果
RnrResponseVO(ret_code=0, ret_msg=成功, serial_number=20181227101254HQFAW236359, timestamp=2018-12-27 02:13:01, response_data={expiredMills=1546481581194, token=196ad86b-cc31})