我有一个包含的类型的混合物象在这个简单的例子反序列化地图<对象,对象>与GSON
final Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("a", 1);
map.put("b", "a");
map.put("c", 2);
final Gson gson = new Gson();
final String string = gson.toJson(map);
final Type type = new TypeToken<LinkedHashMap<String, Object>>(){}.getType();
final Map<Object, Object> map2 = gson.fromJson(string, type);
for (final Entry<Object, Object> entry : map2.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
我会得到什么是纯Object
S,NO Integer
S,NO String
秒的地图。输出看起来像
a : [email protected]
b : [email protected]
c : [email protected]
我可以修复它吗?我期望这种简单的情况下将被默认正确处理。
我知道关于该类型的信息不能总是保留,并且可能1
和"1"
在JSON中的含义完全相同。但是,返回简单的无内容对象对我来说毫无意义。
更新:序列化版本(即上述string
)看起来很好:
{"a":1,"b":"a","c":2}
你可以看看最后的字符串字符串?我相信这有助于理解这个问题。 – 2011-03-22 20:17:22
我刚刚也遇到了这个......这就是为什么我使用我的JSONer ...它可能会慢一点,但它比Gson更通用:http://nu-art-infrastructure.blogspot.co .il/2013/03/jsoner.html – TacB0sS 2013-09-15 08:32:14
@ TacB0sS:我已经遵循了“清晰和静态的数据结构”建议。但是,让Gson处理这个应该是非常简单的。你是否在意提出问题? – maaartinus 2013-09-15 14:52:14