Gson 转换long到科学计数法的解决方法
今天发现从数据库中读取毫秒,然后转成对象的时候,某个字段出现科学计数法了,而且转为对象的时候,明明存储的是long类型,但是变为了double类型,网上的很多方法都不靠谱。最后找到了解决方法,如下。(我这得情况是gson转为对象,对象里面有个字段是hashMap的数据结构)
解决方法
import com.google.gson.*
import java.lang.reflect.Type
class GsonUtils {
static Gson getGson(){
Gson gson=new GsonBuilder().registerTypeAdapter(HashMap.class, new JsonDeserializer<HashMap>() {
public HashMap deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException{
HashMap<String,Object> resultMap=new HashMap<>();
def jsonObject = json.getAsJsonObject()
Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
resultMap.put(entry.getKey(),entry.getValue());
}
return resultMap;
}
}).create();
return gson;
}
}
版权声明:本文为u010648159原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。