如何修改Java中的JsonNode?
How to modify JsonNode in Java?
我需要在 Java 中更改 JSON 属性的值,我可以正确获取该值但无法修改 JSON。
下面是代码
JsonNode blablas = mapper.readTree(parser).get("blablas");
for (JsonNode jsonNode : blablas) {
String elementId = jsonNode.get("element").asText();
String value = jsonNode.get("value").asText();
if (StringUtils.equalsIgnoreCase(elementId, "blabla")) {
if(value != null && value.equals("YES")){
// I need to change the node to NO then save it into the JSON
}
}
}
最好的方法是什么?
您需要获取 ObjectNode
类型的对象才能设置值。
看看this
JsonNode
是不可变的,用于解析操作。但是,它可以转换为允许突变的ObjectNode
(和ArrayNode
):
((ObjectNode)jsonNode).put("value", "NO");
对于数组,您可以使用:
((ObjectNode)jsonNode).putArray("arrayName").add(object.getValue());
我想你可以转换为 ObjectNode 并使用 put
方法。像这样
ObjectNode o = (ObjectNode) jsonNode;
o.put("value", "NO");
只是为了其他人的理解,他们可能无法清楚地了解下面的代码,我可以找到一个字段然后更新它
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(JsonString);
JsonPointer valueNodePointer = JsonPointer.compile("/GrandObj/Obj/field");
JsonPointer containerPointer = valueNodePointer.head();
JsonNode parentJsonNode = rootNode.at(containerPointer);
if (!parentJsonNode.isMissingNode() && parentJsonNode.isObject()) {
ObjectNode parentObjectNode = (ObjectNode) parentJsonNode;
//following will give you just the field name.
//e.g. if pointer is /grandObject/Object/field
//JsonPoint.last() will give you /field
//remember to take out the / character
String fieldName = valueNodePointer.last().toString();
fieldName = fieldName.replace(Character.toString(JsonPointer.SEPARATOR), StringUtils.EMPTY);
JsonNode fieldValueNode = parentObjectNode.get(fieldName);
if(fieldValueNode != null) {
parentObjectNode.put(fieldName, "NewValue");
}
}
@Sharon-Ben-Asher 的回答不错。
但在我的例子中,对于一个数组我必须使用:
((ArrayNode) jsonNode).add("value");
添加一个答案,因为其他一些人在已接受答案的评论中投票赞成他们在尝试转换为 ObjectNode(包括我自己)时遇到此异常:
Exception in thread "main" java.lang.ClassCastException:
com.fasterxml.jackson.databind.node.TextNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
解决方案是获取'parent'节点,并执行put
,有效替换整个节点,无论原始节点类型如何。
如果需要"modify"节点使用节点已有的值:
get
JsonNode
value/array
- 对 value/array
进行修改
- 继续调用父级
put
。
代码,这里的目标是修改subfield
,也就是NodeA
和Node1
的子节点:
JsonNode nodeParent = someNode.get("NodeA")
.get("Node1");
// Manually modify value of 'subfield', can only be done using the parent.
((ObjectNode) nodeParent).put('subfield', "my-new-value-here");
学分:
我的灵感来自 , thanks to wassgreen@
我需要在 Java 中更改 JSON 属性的值,我可以正确获取该值但无法修改 JSON。
下面是代码
JsonNode blablas = mapper.readTree(parser).get("blablas");
for (JsonNode jsonNode : blablas) {
String elementId = jsonNode.get("element").asText();
String value = jsonNode.get("value").asText();
if (StringUtils.equalsIgnoreCase(elementId, "blabla")) {
if(value != null && value.equals("YES")){
// I need to change the node to NO then save it into the JSON
}
}
}
最好的方法是什么?
您需要获取 ObjectNode
类型的对象才能设置值。
看看this
JsonNode
是不可变的,用于解析操作。但是,它可以转换为允许突变的ObjectNode
(和ArrayNode
):
((ObjectNode)jsonNode).put("value", "NO");
对于数组,您可以使用:
((ObjectNode)jsonNode).putArray("arrayName").add(object.getValue());
我想你可以转换为 ObjectNode 并使用 put
方法。像这样
ObjectNode o = (ObjectNode) jsonNode;
o.put("value", "NO");
只是为了其他人的理解,他们可能无法清楚地了解下面的代码,我可以找到一个字段然后更新它
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(JsonString);
JsonPointer valueNodePointer = JsonPointer.compile("/GrandObj/Obj/field");
JsonPointer containerPointer = valueNodePointer.head();
JsonNode parentJsonNode = rootNode.at(containerPointer);
if (!parentJsonNode.isMissingNode() && parentJsonNode.isObject()) {
ObjectNode parentObjectNode = (ObjectNode) parentJsonNode;
//following will give you just the field name.
//e.g. if pointer is /grandObject/Object/field
//JsonPoint.last() will give you /field
//remember to take out the / character
String fieldName = valueNodePointer.last().toString();
fieldName = fieldName.replace(Character.toString(JsonPointer.SEPARATOR), StringUtils.EMPTY);
JsonNode fieldValueNode = parentObjectNode.get(fieldName);
if(fieldValueNode != null) {
parentObjectNode.put(fieldName, "NewValue");
}
}
@Sharon-Ben-Asher 的回答不错。
但在我的例子中,对于一个数组我必须使用:
((ArrayNode) jsonNode).add("value");
添加一个答案,因为其他一些人在已接受答案的评论中投票赞成他们在尝试转换为 ObjectNode(包括我自己)时遇到此异常:
Exception in thread "main" java.lang.ClassCastException:
com.fasterxml.jackson.databind.node.TextNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode
解决方案是获取'parent'节点,并执行put
,有效替换整个节点,无论原始节点类型如何。
如果需要"modify"节点使用节点已有的值:
get
JsonNode
value/array- 对 value/array 进行修改
- 继续调用父级
put
。
代码,这里的目标是修改subfield
,也就是NodeA
和Node1
的子节点:
JsonNode nodeParent = someNode.get("NodeA")
.get("Node1");
// Manually modify value of 'subfield', can only be done using the parent.
((ObjectNode) nodeParent).put('subfield', "my-new-value-here");
学分:
我的灵感来自