本文介绍了如何起价EX pressing整数作为浮点数p $ pvent GSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GSON有一些奇怪的行为,当我尝试将字符串转换为JSON。下面的code字符串转换成草案JSON响应。有没有一种方法,以prevent GSON从加入0.0到所有整数值?

 的ArrayList<哈希表<字符串,对象>>对策;
键入ResponseList =新TypeToken&其中; ArrayList的&其中;哈希表&所述;串,对象>>>(){} .getType();
。反应=新GSON()fromJson(草案ResponseList);

草案:
[{ID:4077395,FIELD_ID:242566,身:},
  {ID:4077398,FIELD_ID:242569,身体:[273019,0],[273020,1],[273021,0]},
  {ID:4077399,FIELD_ID:242570,身体:[273022,0],[273023,1],[273024,0]}
]

对策:
[{ID = 4077395.0,身体=,FIELD_ID = 242566.0},
  {的id = 4077398.0,身体= [[273019.0,0.0],[273020.0,1.0],[273021.0,0.0],FIELD_ID = 242569.0},
  {的id = 4077399.0,身体= [[273022.0,0.0],[273023.0,1.0],[273024.0,0.0],FIELD_ID = 242570.0}
]
 

解决方案

你告诉GSON它寻找的字符串映射到对象名单,基本上说,它使一个最佳猜测的类型目的。由于浮点/双精度比整数更通用的,这是因为它使用一个合理的选择。

GSON基本构建检查要以确定如何解析数据来填充的对象的类型。如果你不给它任何暗示,它不会很好地工作。一种选择是定义一个自定义JsonDeserializer,但更好的办法是不使用HashMap(绝对不要使用Hashtable的!),而是给出GSON有关数据的该公司预计,该类型的详细信息。

 级别响应{
  INT ID;
  INT FIELD_ID;
  ArrayList的< ArrayList的<整数GT;>体; //或任何类型是最apropriate
}

响应=新GSON()
            .fromJson(草案新TypeToken< ArrayList的<响应>>(){}的getType());
 

再次GSON的全部意义在于无缝地转换结构化数据转换成结构化的对象。如果你问它来创建一个几乎不确定的结构是怎样的对象映射列表,你击败GSON整点,还不如使用一些更简单的JSON解析器。

Gson has some odd behavior when I try to convert a string to json. The code below transforms string draft into json responses. Is there a way to prevent gson from adding the '.0 to all integer values?

ArrayList<Hashtable<String, Object>> responses;
Type ResponseList = new TypeToken<ArrayList<Hashtable<String, Object>>>() {}.getType();
responses = new Gson().fromJson(draft, ResponseList);

draft:
[ {"id":4077395,"field_id":242566,"body":""},
  {"id":4077398,"field_id":242569,"body":[[273019,0],[273020,1],[273021,0]]},
  {"id":4077399,"field_id":242570,"body":[[273022,0],[273023,1],[273024,0]]}
]

responses:
[ {id=4077395.0, body=, field_id=242566.0},
  {id=4077398.0, body=[[273019.0, 0.0], [273020.0, 1.0], [273021.0, 0.0]], field_id=242569.0},
  {id=4077399.0, body=[[273022.0, 0.0], [273023.0, 1.0], [273024.0, 0.0]], field_id=242570.0}
]
解决方案

You're telling Gson it's looking for a list of maps of Strings to Objects, which essentially says for it to make a best guess as to the type of the Object. Since Float/Double is more generic than Integer, it's a logical option for it to use.

Gson is fundamentally built to inspect the type of the object you want to populate in order to determine how to parse the data. If you don't give it any hint, it's not going to work very well. One option is to define a custom JsonDeserializer, however better would be to not use a HashMap (and definitely don't use Hashtable!) and instead give Gson more information about the type of data it's expecting.

class Response {
  int id;
  int field_id;
  ArrayList<ArrayList<Integer>> body; // or whatever type is most apropriate
}

responses = new Gson()
            .fromJson(draft, new TypeToken<ArrayList<Response>>(){}.getType());

Again, the whole point of Gson is to seamlessly convert structured data into structured objects. If you ask it to create a nearly undefined structure like a list of maps of objects, you're defeating the whole point of Gson, and might as well use some more simplistic JSON parser.

这篇关于如何起价EX pressing整数作为浮点数p $ pvent GSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:25