【发布时间】:2015-09-08 22:22:34
【问题描述】:
我需要在 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
}
}
}
最好的方法是什么?
【问题讨论】:
-
您可以将 JsonNode 转换为 Java Map,例如
resultMap = mapper.convertValue(aJsonNode, Map.class);在 Map 中修改它,然后将该 Map 改回 JsonNode。只是说。