问题描述
我创建了一个新的JsonNode
I have a new JsonNode that I created
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.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 $ c $中的突变c>。
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中创建插入新节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!