How to modify JsonNode in Java?

后端 未结 6 742
生来不讨喜
生来不讨喜 2020-11-28 22:29

I need to change a JSON attribute\'s value in Java, I can get the value properly but I couldn\'t modify the JSON.

here is the code below

  JsonNode          


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 22:51

    JsonNode is immutable and is intended for parse operation. However, it can be cast into ObjectNode (and ArrayNode) that allow mutations:

    ((ObjectNode)jsonNode).put("value", "NO");
    

    For an array, you can use:

    ((ObjectNode)jsonNode).putArray("arrayName").add(object.ge‌​tValue());
    

提交回复
热议问题