Java Jackson JSON ObjectMapper.readvalue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24286517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:18:04  来源:igfitidea点击:

Hymanson JSON ObjectMapper.readvalue

javajsonHymanson

提问by chipmunk

I'm going through code examples on converting the Java object to a JSON and I came across this:

我正在查看将 Java 对象转换为 JSON 的代码示例,我遇到了这个:

HashMap<String, Object> filters = new ObjectMapper().readValue(filterStr, HashMap.class);

where

在哪里

String filterStr;

sorry, but what exactly is the above line of code doing? I went through other example here.I can see that readValue() has been overridden but how can a string be converted to a HashMap? Shouldn't it be a JSON object and not a string? Thanks.

抱歉,上面这行代码到底在做什么?我在这里经历了其他例子我可以看到 readValue() 已被覆盖,但是如何将字符串转换为 HashMap?它不应该是一个 JSON 对象而不是一个字符串吗?谢谢。

采纳答案by Nikhil Talreja

ObjectMapper().readValue()

is overloaded to do several types of conversions.

被重载以进行多种类型的转换。

If the filterStr is compatible to be converted to a HashMap this will method will do it.

如果 filterStr 兼容转换为 HashMap,则此方法将执行此操作。

E.g. filterStr = "{\"name\":\"Tom\", \"age\":\"25\"}";will give a map with key-value pairs as {age=25, name=Tom}

例如filterStr = "{\"name\":\"Tom\", \"age\":\"25\"}";将给出一个键值对为 {age=25, name=Tom} 的映射