本文介绍了反序列化映射< Object,Object>与GSon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个包含混合类型的Map,就像这个简单的例子一样 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 = 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 整数 s,no String s。输出如下所示: a:java.lang.Object@48d19bc8 b:java.lang.Object@394a8cd1 c:java.lang.Object@4d630ab9 我可以以某种方式修复它吗?我希望这种简单的情况会在默认情况下正确处理。 更新:序列化版本(即 $ p $ {a:1,code> string above)看起来不错: b:a,c:2} 解决方案 例如 public class Data { private Integer a; 私人字符串b; 私人整数c; // ... } 结合 Data data1 = new Data(1,a,2); String json = gson.toJson(data1); Data data2 = gson.fromJson(json,Data.class); 更新:as根据评论,键集看起来不是固定的(尽管事先不知道结构,你似乎可以手动将其转换)。您可以创建自定义解串器。这里有一个quick'n'dirty的例子。 public class ObjectDeserializer实现了JsonDeserializer< Object> { $ b $ @Override public Object deserialize(JsonElement element,Type type,JsonDeserializationContext context)throws JsonParseException { String value = element.getAsString(); 尝试{ return Long.valueOf(value); } catch(NumberFormatException e){返回值; $ b $ / code> 你使用如下: final Gson gson = new GsonBuilder()。registerTypeAdapter(Object.class,new ObjectDeserializer())。create (); // ... I have a Map containing a mixture of types like in this simple examplefinal 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());}What I get back are plain Objects, no Integers, no Strings. The output looks likea : java.lang.Object@48d19bc8b : java.lang.Object@394a8cd1c : java.lang.Object@4d630ab9Can I fix it somehow? I'd expect that such simple cases will be handled correctly by default.Update: The serialized version (i.e. the string above) looks fine:{"a":1,"b":"a","c":2} 解决方案 Gson isn't that smart. Rather provide a clear and static data structure in flavor of a Javabean class so that Gson understands what type the separate properties are supposed to be deserialized to.E.g.public class Data { private Integer a; private String b; private Integer c; // ...}in combination withData data1 = new Data(1, "a", 2);String json = gson.toJson(data1);Data data2 = gson.fromJson(json, Data.class);Update: as per the comments, the keyset seems to be not fixed (although you seem to be able to convert it manually afterwards without knowing the structure beforehand). You could create a custom deserializer. Here's a quick'n'dirty example.public class ObjectDeserializer implements JsonDeserializer<Object> { @Override public Object deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { String value = element.getAsString(); try { return Long.valueOf(value); } catch (NumberFormatException e) { return value; } }}which you use as follows:final Gson gson = new GsonBuilder().registerTypeAdapter(Object.class, new ObjectDeserializer()).create();// ... 这篇关于反序列化映射< Object,Object>与GSon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 11:38