如何在 JsonNode 中创建插入新节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11503604/
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
how to create insert new nodes in JsonNode?
提问by Dexter Cato
I have a new JsonNode that I created
我创建了一个新的 JsonNode
JsonNode jNode = new ObjectCodec().createObjectNode();
with this node, how do I then add key value pairs within so that I can construct this new node with the new values? What I read in http://www.cowtowncoder.com/blog/archives/2011/08/entry_460.htmlmentioned about using
有了这个节点,我如何在其中添加键值对,以便我可以用新值构造这个新节点?我在http://www.cowtowncoder.com/blog/archives/2011/08/entry_460.html中读到的内容提到了使用
jNode.with("newNode").put("key1","value1");
But looking at the APIs for Hymanson's JsonNode (v1.8) does not show any method as such.
但是查看 Hymanson 的 JsonNode (v1.8) 的 API 并没有显示任何方法。
回答by StaxMan
These methods are in ObjectNode: the division is such that most read operations are included in JsonNode, but mutations in ObjectNodeand ArrayNode.
这些方法是ObjectNode:除法使得大多数读取操作包含在 中JsonNode,但在ObjectNode和中包含突变ArrayNode。
Note that you can just change first line to be:
请注意,您可以将第一行更改为:
ObjectNode jNode = mapper.createObjectNode();
// version ObjectMapper has should return ObjectNode type
or
或者
ObjectNode jNode = (ObjectNode) objectCodec.createObjectNode();
// ObjectCodec is in core part, must be of type JsonNode so need cast
回答by rand0m86
I've recently found even more interesting way to create any ValueNodeor ContainerNode(Hymanson v2.3).
我最近发现了更有趣的方式来创建 any ValueNodeor ContainerNode(Hymanson v2.3)。
ObjectNode node = JsonNodeFactory.instance.objectNode();

