如何将JSON字符串转换为具有Jackson JSON的Map >(How to convert a JSON string to a Map> with Jackson JSON)

编程入门 行业动态 更新时间:2024-10-28 14:22:56
如何将JSON字符串转换为具有Jackson JSON的Map >(How to convert a JSON string to a Map> with Jackson JSON)

我知道通过以下方式将JSON字符串转换为Map<String, String>实现:

public <T1, T2> HashMap<T1, T2> getMapFromJson(String json, Class<T1> keyClazz, Class<T2> valueClazz) throws TMMIDConversionException { if (StringUtils.isEmpty(json)) { return null; } try { ObjectMapper mapper = getObjectMapper(); HashMap<T1, T2> map = mapper.readValue(json, TypeFactory.defaultInstance().constructMapType(HashMap.class, keyClazz, valueClazz)); return map; } catch (Exception e) { Logger.error(e.getMessage(), e.getCause()); } }

但我无法扩展它来将我的JSON转换为Map<String, Set<String>> 。 显然,上面的方法失败了,因为它打破了Set项并放入列表中。 在这里需要一些帮助! 谢谢

示例JSON字符串如下所示。 这个JSOn必须转换成一个Map<String, Set<CustomClass>> 。

{ "0": [ { "cid": 100, "itemId": 0, "position": 0 } ], "1": [ { "cid": 100, "itemId": 1, "position": 0 } ], "7": [ { "cid": 100, "itemId": 7, "position": -1 }, { "cid": 140625, "itemId": 7, "position": 1 } ], "8": [ { "cid": 100, "itemId": 8, "position": 0 } ], "9": [ { "cid": 100, "itemId": 9, "position": 0 } ] }

I am aware of the implementation which converts JSON string to a Map<String, String> through :

public <T1, T2> HashMap<T1, T2> getMapFromJson(String json, Class<T1> keyClazz, Class<T2> valueClazz) throws TMMIDConversionException { if (StringUtils.isEmpty(json)) { return null; } try { ObjectMapper mapper = getObjectMapper(); HashMap<T1, T2> map = mapper.readValue(json, TypeFactory.defaultInstance().constructMapType(HashMap.class, keyClazz, valueClazz)); return map; } catch (Exception e) { Logger.error(e.getMessage(), e.getCause()); } }

But I cannot extend it to convert my JSON to a Map<String, Set<String>>. The above method fails, obviously, as it breaks the Set items and puts in the list. Need some help here!! Thanks

Sample JSON String is as below. This JSOn has to converted to a Map<String, Set<CustomClass>>.

{ "0": [ { "cid": 100, "itemId": 0, "position": 0 } ], "1": [ { "cid": 100, "itemId": 1, "position": 0 } ], "7": [ { "cid": 100, "itemId": 7, "position": -1 }, { "cid": 140625, "itemId": 7, "position": 1 } ], "8": [ { "cid": 100, "itemId": 8, "position": 0 } ], "9": [ { "cid": 100, "itemId": 9, "position": 0 } ] }

最满意答案

尝试这个:

JavaType setType = mapper.getTypeFactory().constructCollectionType(Set.class, CustomClass.class); JavaType stringType = mapper.getTypeFactory().constructType(String.class); JavaType mapType = mapper.getTypeFactory().constructMapType(Map.class, stringType, setType); String outputJson = mapper.readValue(json, mapType)

Try this:

JavaType setType = mapper.getTypeFactory().constructCollectionType(Set.class, CustomClass.class); JavaType stringType = mapper.getTypeFactory().constructType(String.class); JavaType mapType = mapper.getTypeFactory().constructMapType(Map.class, stringType, setType); String outputJson = mapper.readValue(json, mapType)

更多推荐

本文发布于:2023-04-28 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   转换为   如何将   JSON   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!