La risposta breve è: non è possibile. TextNode
non espone alcuna operazione che consente di modificare il contenuto.
Con ciò detto, è possibile attraversare facilmente i nodi in un ciclo o tramite ricorsione per ottenere il comportamento desiderato. Immaginate la seguente:
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);
}
}
L'output è:
{ "fieldName": "nuovo valore", "annidati": { "fieldName": "nuovo valore"}}
Hai visto http://stackoverflow.com/questions/17967531/jackson-api-partially-update-a-string –
Non ho POJO che rappresenta la mia JSON. Devo davvero fare tutto questo per aggiornare un semplice textNode ?? !! –
Vedere anche: http://wiki.fasterxml.com/JacksonHowToCustomDeserializers e http://www.baeldung.com/jackson-deserialization –