我有这样的json ArrayNode,我需要从每个元素(例如,使用ObjectMapper、ArrayNode、JsonNode或ObjectNode的字段"xxx" )中删除。但没有Gson和@JsonIgnore等
"arrayNode": [
{
"xxx": {},
"yyy": {}
},
{
"xxx": {},
"yyy": {}
}
]
发布于 2019-12-03 01:49:52
我不知道这个问题是否已经解决。但是下面的代码片段显示了如何从JSON节点中删除键为xxx
的字段。JsonNode
无法执行插入或删除,因此您必须将其转换为ObjectNode
以进行进一步的操作。
代码片段
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(jsonStr);
rootNode.get("arrayNode").forEach(e -> {
if (e.has("xxx")) {
ObjectNode objNode = (ObjectNode) e;
objNode.remove("xxx");
}
});
System.out.println(rootNode.toString());
控制台输出
{“arrayNode”:{“yyy”:{},{“yyy”:{}}
发布于 2016-06-02 14:36:20
您可以使用这个maven依赖项:http://mvnrepository.com/artifact/org.json/json/20160212
它很简单,很容易被低估和使用。例:
JSONObject obj = "YOUR_JSON_STRING";
JSONArray result = obj.getJSONArray("YOUR_STRING_KEY");
for(JSONObject elem : result){
String out = elem.getString("xxx");
}
更多你可以读到的:https://developer.android.com/reference/org/json/JSONArray.html祝你好运
https://stackoverflow.com/questions/37594667
复制相似问题