java 从 JSON 字符串创建一个 ObjectNode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30088573/
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
Create an ObjectNode from JSON string
提问by Petah
How do I create a ObjectNode from a string using Hymanson?
如何使用 Hymanson 从字符串创建 ObjectNode?
I tried:
我试过:
ObjectNode json = new ObjectMapper().readValue("{}", ObjectNode.class);
But get
但是得到
Exception in thread "main" com.fasterxml.Hymanson.databind.JsonMappingException: Conflicting setter definitions for property "type": jdk.nashorn.internal.ir.Symbol#setType(1 params) vs jdk.nashorn.internal.ir.Symbol#setType(1 params)
线程“main”中的异常 com.fasterxml.Hymanson.databind.JsonMappingException:属性“type”的 setter 定义冲突:jdk.nashorn.internal.ir.Symbol#setType(1 params) vs jdk.nashorn.internal.ir.Symbol #setType(1 参数)
I want to be able to read a JSON string the add/modify some values.
我希望能够读取 JSON 字符串添加/修改一些值。
回答by Petah
You are using the wrong import.
您正在使用错误的导入。
It should be
它应该是
com.fasterxml.Hymanson.databind.node.ObjectNode
Not:
不是:
jdk.nashorn.internal.ir.ObjectNode
回答by skaffman
Firstly, the error message suggests you're tying to build a jdk.nashorn.internal.ir.ObjectNode, whereas I'm guessing you actually intended to build a com.fasterxml.Hymanson.databind.node.ObjectNode(for Hymanson 2.x). Check your imports.
首先,错误消息表明您要构建一个jdk.nashorn.internal.ir.ObjectNode,而我猜您实际上打算构建一个com.fasterxml.Hymanson.databind.node.ObjectNode(对于 Hymanson 2.x)。检查您的进口。
However, if all you want to do is build an empty ObjectNode, then just use
但是,如果您只想构建一个空的ObjectNode,那么只需使用
JsonNodeFactory.instance.objectNode()
If for some reason you reallywant to do it by parsing an empty JSON object, then use this:
如果出于某种原因你真的想通过解析一个空的 JSON 对象来做到这一点,那么使用这个:
ObjectNode json = (ObjectNode) new ObjectMapper().readTree("{}");
But that's just unpleasant.
但这只是不愉快。

