Modify JsonNode of unknown JSON dynamically in Java

落花浮王杯 提交于 2019-12-01 18:45:26

JsonNode objects are immutable so you can't modify them. What you can do is replace a JsonNode with another one. A cast to ObjectNode is also required to expose the required methods. First find the parent of the node you want to replace :

JsonNode node = root.findParent("JsonPath");

Then use either of these 2 methods to replace it with a new one:

((ObjectNode) node).remove("JsonPath");           // remove current node
((ObjectNode) node).put("JsonPath", "New Value"); // add new one with new value

or

((ObjectNode) node).replace("JsonPath", new TextNode("New Value"));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!