我的json对象里面有任意值.我想在Map中反序列化它.除了将整数转换为双打外,一切都很好.见例子:
{"id":1, "inner_obj":{"key":"value","num":666,"map":{"key":"value"}}}
Run Code Online (Sandbox Code Playgroud)
反序列化为this(map.toString()):
{id=1.0, inner_obj={key=value, num=666.0, map={key=value}}}
Run Code Online (Sandbox Code Playgroud)
是否有一些简单的方法将"id"和"num"反序列化为整数而不是双打?
JSON中没有整数类型.1和1.0是相同的.您需要在代码中解析1.0到1.或者您需要将JSON映射到某个VO类并明确定义类的字段类型,以便GSON可以理解您要查找的内容.
我自己一直在寻找嵌套 Map 问题的解决方案,上面的“\xec\x9d\xb4\xec\xa2\x85\xec\x9d\x80”是第一个在非平凡用例中真正有所帮助的答案。
\n\n由于上面的解决方案仅处理数字,因此我更新了该解决方案以提供字符串和布尔值的通用解析功能,请参阅下面更新的代码:
\n\nprivate static class MapDeserializer implements JsonDeserializer<Map<String, Object>> {\n\n public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {\n Map<String, Object> m = new LinkedHashMap<String, Object>();\n JsonObject jo = json.getAsJsonObject();\n for (Entry<String, JsonElement> mx : jo.entrySet()) {\n String key = mx.getKey();\n JsonElement v = mx.getValue();\n if (v.isJsonArray()) {\n m.put(key, g.fromJson(v, List.class));\n } else if (v.isJsonPrimitive()) {\n Number num = null;\n ParsePosition position=new ParsePosition(0);\n String vString=v.getAsString();\n try {\n num = NumberFormat.getInstance(Locale.ROOT).parse(vString,position);\n } catch (Exception e) {\n }\n //Check if the position corresponds to the length of the string\n if(position.getErrorIndex() < 0 && vString.length() == position.getIndex()) {\n if (num != null) {\n m.put(key, num);\n continue;\n }\n }\n JsonPrimitive prim = v.getAsJsonPrimitive();\n if (prim.isBoolean()) {\n m.put(key, prim.getAsBoolean());\n } else if (prim.isString()) {\n m.put(key, prim.getAsString());\n } else {\n m.put(key, null);\n }\n\n } else if (v.isJsonObject()) {\n m.put(key, g.fromJson(v, Map.class));\n }\n\n }\n return m;\n }\n}\n\nprivate static class ListDeserializer implements JsonDeserializer<List<Object>> {\n\n public List<Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {\n List<Object> m = new ArrayList<Object>();\n JsonArray arr = json.getAsJsonArray();\n for (JsonElement jsonElement : arr) {\n if (jsonElement.isJsonObject()) {\n m.add(g.fromJson(jsonElement, Map.class));\n } else if (jsonElement.isJsonArray()) {\n m.add(g.fromJson(jsonElement, List.class));\n } else if (jsonElement.isJsonPrimitive()) {\n Number num = null;\n try {\n num = NumberFormat.getInstance().parse(jsonElement.getAsString());\n } catch (Exception e) {\n }\n if (num != null) {\n m.add(num);\n continue;\n }\n JsonPrimitive prim = jsonElement.getAsJsonPrimitive();\n if (prim.isBoolean()) {\n m.add(prim.getAsBoolean());\n } else if (prim.isString()) {\n m.add(prim.getAsString());\n } else {\n m.add(null);\n }\n }\n }\n return m;\n }\n}\n\nprivate static Gson g = new GsonBuilder().registerTypeAdapter(Map.class, new MapDeserializer()).registerTypeAdapter(List.class, new ListDeserializer()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
15101 次 |
| 最近记录: |