I am not sure whether this problem has been solved or not. But following code snippet shows how to remove a field whose key is xxx from JSON node. And a JsonNode cannot perform insertion or deletion, so you have to cast it to ObjectNode for further manipulation.
Code snippet
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());
Console output
Answer from LHCHIN on Stack Overflow{"arrayNode":[{"yyy":{}},{"yyy":{}}]}
I am not sure whether this problem has been solved or not. But following code snippet shows how to remove a field whose key is xxx from JSON node. And a JsonNode cannot perform insertion or deletion, so you have to cast it to ObjectNode for further manipulation.
Code snippet
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());
Console output
{"arrayNode":[{"yyy":{}},{"yyy":{}}]}
You can use this maven dependency : http://mvnrepository.com/artifact/org.json/json/20160212
It's very simple to understated and use. ex:
JSONObject obj = "YOUR_JSON_STRING";
JSONArray result = obj.getJSONArray("YOUR_STRING_KEY");
for(JSONObject elem : result){
String out = elem.getString("xxx");
}
More you can read at : https://developer.android.com/reference/org/json/JSONArray.html Good luck
Removing an element from an Array (Java) - Stack Overflow
DocumentContext.delete removes extra elements from array
java - Remove object from JSON array - Stack Overflow
java - How to remove empty node in JsonNode - Stack Overflow
Videos
You could use commons lang's ArrayUtils.
array = ArrayUtils.removeElement(array, element)
commons.apache.org library:Javadocs
Your question isn't very clear. From your own answer, I can tell better what you are trying to do:
public static String[] removeElements(String[] input, String deleteMe) {
List result = new LinkedList();
for(String item : input)
if(!deleteMe.equals(item))
result.add(item);
return result.toArray(input);
}
NB: This is untested. Error checking is left as an exercise to the reader (I'd throw IllegalArgumentException if either input or deleteMe is null; an empty list on null list input doesn't make sense. Removing null Strings from the array might make sense, but I'll leave that as an exercise too; currently, it will throw an NPE when it tries to call equals on deleteMe if deleteMe is null.)
Choices I made here:
I used a LinkedList. Iteration should be just as fast, and you avoid any resizes, or allocating too big of a list if you end up deleting lots of elements. You could use an ArrayList, and set the initial size to the length of input. It likely wouldn't make much of a difference.
If jsonNode is an Array of Objects then jsonNode.elements() returns Iterator<JsonNode>, by using if condition check the node with name equals 1 then delete the entire node
JsonNode jsonNode = objectMapper.readTree(new File("savefiles.json"));
Iterator<JsonNode> nodes = jsonNode.elements()
while(nodes.hasNext()) {
if(nodes.next().get("name").textValue().equals("1")){
nodes.remove();
}
}
If you want to remove an element from the ArrayNode, just
final JsonNode json = objectMapper.readTree(new File("savefiles.json"));
if (json.isArray()) {
for (final Iterator<JsonNode> i = json.elements(); i.hasNext(); ) {
final JsonNode jsonNode = i.next();
if ("1".equals(jsonNode.get("name").asText())) {
i.remove();
}
}
}
This will not create a new instance. The original TreeNode (which is an ArrayNode) is maintained.
Note also the .asText() while comparing.
A Stream version is
final ArrayNode filtered =
StreamSupport.stream(json.spliterator(), false)
.filter(e -> !"1".equals(e.get("name").asText()))
.collect(Collector.of(
objectMapper::createArrayNode,
(array, element) -> array.add(element),
(result, toMerge) -> result.addAll(toMerge)
));