How to deserialize map using integer keys in Java
•
Java
I've been using the flexjson API to serialize and deserialize maps using integer keys
This is a sample code block
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)); }
Deserialization does not produce any errors, but the key is converted to string
Solution
This can be like genson:
Genson genson = new Genson(); Map<Integer,Object> result = genson.deserialize(json,new GenericType<Map<Integer,Object>>() {});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码