如何用另一个(更新)替换Java Jackson TextNode ?

时间:2021-11-09 21:08:23

My goal is to update some textual fields in a JsonNode.

我的目标是更新JsonNode中的一些文本字段。

    List<JsonNode> list = json.findValues("fieldName");
    for(JsonNode n : list){
        // n is a TextNode. I'd like to change its value.
    }

I don't see how this could be done. Do you have any suggestion?

我不知道怎么做。你有什么建议吗?

2 个解决方案

#1


14  

The short answer is: you can't. TextNode does not expose any operations that allows you to alter the contents.

简短的回答是:你不能。TextNode不公开任何允许您修改内容的操作。

With that being said, you can easily traverse the nodes in a loop or via recursion to get the desired behaviour. Imagine the following:

说到这里,您可以轻松地遍历循环中的节点或通过递归获得所需的行为。想象一下:

public class JsonTest {
    public static void change(JsonNode parent, String fieldName, String newValue) {
        if (parent.has(fieldName)) {
            ((ObjectNode) parent).put(fieldName, newValue);
        }

        // Now, recursively invoke this method on all properties
        for (JsonNode child : parent) {
            change(child, fieldName, newValue);
        }
    }

    @Test
    public static void main(String[] args) throws IOException {
        String json = "{ \"fieldName\": \"Some value\", \"nested\" : { \"fieldName\" : \"Some other value\" } }";
        ObjectMapper mapper = new ObjectMapper();
        final JsonNode tree = mapper.readTree(json);
        change(tree, "fieldName", "new value");
        System.out.println(tree);
    }
}

The output is:

的输出是:

{"fieldName":"new value","nested":{"fieldName":"new value"}}

{“字段名”:“新价值”,“嵌套”:{“字段名”:“新价值”} }

#2


1  

Because you cannot modify a TextNode, you can instead get all the parentNodes of that field, and call the put operation on it with the same field name and a new value. It will replace the existing field and change the value.

因为不能修改TextNode,所以可以获取该字段的所有parentnode,并使用相同的字段名和新值调用该字段的put操作。它将替换现有字段并更改值。

List<JsonNode> parentNodes = jsonNode.findParents("fieldName");
if(parentNodes != null) {
  for(JsonNode parentNode : parentNodes){
    ((ObjectNode)parentNode).put("fieldName", "newValue");
  }
}

#1


14  

The short answer is: you can't. TextNode does not expose any operations that allows you to alter the contents.

简短的回答是:你不能。TextNode不公开任何允许您修改内容的操作。

With that being said, you can easily traverse the nodes in a loop or via recursion to get the desired behaviour. Imagine the following:

说到这里,您可以轻松地遍历循环中的节点或通过递归获得所需的行为。想象一下:

public class JsonTest {
    public static void change(JsonNode parent, String fieldName, String newValue) {
        if (parent.has(fieldName)) {
            ((ObjectNode) parent).put(fieldName, newValue);
        }

        // Now, recursively invoke this method on all properties
        for (JsonNode child : parent) {
            change(child, fieldName, newValue);
        }
    }

    @Test
    public static void main(String[] args) throws IOException {
        String json = "{ \"fieldName\": \"Some value\", \"nested\" : { \"fieldName\" : \"Some other value\" } }";
        ObjectMapper mapper = new ObjectMapper();
        final JsonNode tree = mapper.readTree(json);
        change(tree, "fieldName", "new value");
        System.out.println(tree);
    }
}

The output is:

的输出是:

{"fieldName":"new value","nested":{"fieldName":"new value"}}

{“字段名”:“新价值”,“嵌套”:{“字段名”:“新价值”} }

#2


1  

Because you cannot modify a TextNode, you can instead get all the parentNodes of that field, and call the put operation on it with the same field name and a new value. It will replace the existing field and change the value.

因为不能修改TextNode,所以可以获取该字段的所有parentnode,并使用相同的字段名和新值调用该字段的put操作。它将替换现有字段并更改值。

List<JsonNode> parentNodes = jsonNode.findParents("fieldName");
if(parentNodes != null) {
  for(JsonNode parentNode : parentNodes){
    ((ObjectNode)parentNode).put("fieldName", "newValue");
  }
}