简短的答案是:您不能。
TextNode不公开允许您更改内容的任何操作。
话虽如此,您可以轻松地在循环中或通过递归遍历节点以获得所需的行为。想象以下情况:
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); }}输出为:
{“ fieldName”:“新值”,“嵌套”:{“ fieldName”:“新值”}}



