GSON。将整数反序列化为整数而不是双精度 [英] Gson. Deserialize integers as integers and not as doubles

查看:430
本文介绍了GSON。将整数反序列化为整数而不是双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在里面有任意的json对象。我想在一个Map中反序列化它。除了将整数转换为双打之外,一切都可以。请参阅示例:

pre $ {code> {id:1,inner_obj:{key:value,num :666,map:{key:value}}}

对此进行反序列化(map.toString()):

  {id = 1.0,inner_obj = {key = value,num = 666.0,map = {key = value}}} 

是否有反序列化id和num作为整数而不是双精度?

解决方案

JSON中没有整数类型。 1和1.0是一样的。你需要在代码中解析1.0到1。或者您需要将JSON映射到某个VO类,并明确定义类的字段类型,以便GSON可以理解您要查找的内容。


I have json object with arbitary values inside. And I want to deserialize it in a Map. Everything is ok except converting integers to a doubles. See example:

{"id":1, "inner_obj":{"key":"value","num":666,"map":{"key":"value"}}}

deserializes to this(map.toString()):

{id=1.0, inner_obj={key=value, num=666.0, map={key=value}}}

Is there some easy way to deserialize "id" and "num" as Integers and not as Doubles?

解决方案

There are no integer type in JSON. 1 and 1.0 are the same. You need to parse that 1.0 to 1 in your code. Or you need to map the JSON to some VO class and define the type of fields of the class explicitly , so that GSON can understand what you are looking for.

这篇关于GSON。将整数反序列化为整数而不是双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆