如何在java中使用整数键反序列化Map
作者:互联网
我一直在使用flexjson api来使用整数键序列化和反序列化地图.
现在,反序列化映射键转换为字符串.是否有某些方法可以将键保留为整数.
这是一个示例代码块
Map<Integer,Object> map = new HashMap<Integer, Object>();
map.put(1, "a");
map.put(2,"b");
flexjson.JSONSerializer serializer = new flexjson.JSONSerializer();
String serializedMapString = serializer.serialize(map);
flexjson.JSONDeserializer<Map<Integer,Object>> deserializer = new flexjson.JSONDeserializer<Map<Integer,Object>>();
Map<Integer,Object> deserializedMap = deserializer.deserialize(serializedMapString);
for(Integer key: deserializedMap.keySet()){
System.out.println(key+"-"+deserializedMap.get(key));
}
反序列化不会产生任何错误,但键会转换为String.
解决方法:
这可以像Genson那样:
Genson genson = new Genson();
Map<Integer, Object> result = genson.deserialize(json, new GenericType<Map<Integer, Object>>() {});
标签:json,java,serialization,deserialization,flexjson 来源: https://codeday.me/bug/20190709/1417061.html