如何在 JsonNode 中创建插入新节点? [英] how to create insert new nodes in JsonNode?
问题描述
我创建了一个新的 JsonNode
I have a new JsonNode that I created
JsonNode jNode = new ObjectCodec().createObjectNode();
对于这个节点,我如何在其中添加键值对,以便我可以用新值构造这个新节点?我在 http://www.cowtowncoder.com/blog/archives/2011/08/中读到的内容entry_460.html 提到了使用
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.html mentioned about using
jNode.with("newNode").put("key1","value1");
但是查看 Jackson 的 JsonNode (v1.8) 的 API 并没有显示任何方法.
But looking at the APIs for Jackson's JsonNode (v1.8) does not show any method as such.
推荐答案
这些方法在 ObjectNode
中:划分使得大多数读取操作都包含在 JsonNode
中,但是 ObjectNode
和 ArrayNode
中的突变.
These methods are in ObjectNode
: the division is such that most read operations are included in JsonNode
, but mutations in ObjectNode
and ArrayNode
.
请注意,您可以将第一行更改为:
Note that you can just change first line to be:
ObjectNode jNode = mapper.createObjectNode();
// version ObjectMapper has should return ObjectNode type
或
ObjectNode jNode = (ObjectNode) objectCodec.createObjectNode();
// ObjectCodec is in core part, must be of type JsonNode so need cast
这篇关于如何在 JsonNode 中创建插入新节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!