Remove object from JSON array

Terchila Marian

I have an JSON array who looks like this:

[ {
  "name" : "1",
  "date" : "30/03 19:36:20"
}, {
  "name" : "12",
  "date" : "30/03 19:36:21"
}, {
  "name" : "123",
  "date" : "30/03 19:36:22"
}, {
  "name" : "1234",
  "date" : "30/03 19:36:23"
}, {
  "name" : "12345",
  "date" : "30/03 19:36:25"
} ]

How could I possibly delete one object by its name in java, like let's suppose I wanna delete the 1

 {
  "name" : "1",
  "date" : "30/03 19:36:20"
},

How could I possibly delete just those lines, because my code at the moment deletes all entries from the file

public static void deleteSavefile() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonNode = objectMapper.readTree(new File("savefiles.json"));
    for (JsonNode node : jsonNode) {
        ((ObjectNode)node).remove("name");
        ((ObjectNode)node).remove("date");
    }
    objectMapper.writeValue(new File("savefiles.json"), jsonNode);
}
Deadpool

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();
           }
       }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Remove object from array

분류에서Dev

Asynchronously remove JSON object from screen

분류에서Dev

Removing json Array from json object

분류에서Dev

fetch json object from json array with a key

분류에서Dev

Javascript: Remove object from array on function call to object

분류에서Dev

Check if bullet object has left the screen and remove from array

분류에서Dev

Python. How to efficiently remove custom object from array

분류에서Dev

Javascript - count and remove from an object

분류에서Dev

Retrieval of title from json from indexed object array ios using swiftyjson

분류에서Dev

Remove array key from multidimensional array

분류에서Dev

Remove or Find an object with Mongoose when it is in an array by the object property value

분류에서Dev

How to remove an array element in json-c?

분류에서Dev

How to remove an array element in json-c?

분류에서Dev

How to remove object with specific name from ArrayList

분류에서Dev

How to remove single object from the sessionStorage (AngularJs)

분류에서Dev

ngResource remove record from query object with $delete

분류에서Dev

I cannot pull data from JSON object- array after ajax post?

분류에서Dev

Keep getting [object, Object] from array for $.each

분류에서Dev

How to remove a row from the javascript object based on a field and realign the object

분류에서Dev

Remove duplicate hashes from two array in js?

분류에서Dev

Remove whitespace from all array elements

분류에서Dev

how to remove file details from multi array

분류에서Dev

Remove Last Comma From Array Value in Loop

분류에서Dev

How do I remove values from an array?

분류에서Dev

How to remove \0 from the array of strings

분류에서Dev

Remove the item from array in javascript titanium

분류에서Dev

Remove value from array in magento field

분류에서Dev

Imploding in PHP an array from Json

분류에서Dev

Parsing JSON Object inside Array in Jackson

Related 관련 기사

  1. 1

    Remove object from array

  2. 2

    Asynchronously remove JSON object from screen

  3. 3

    Removing json Array from json object

  4. 4

    fetch json object from json array with a key

  5. 5

    Javascript: Remove object from array on function call to object

  6. 6

    Check if bullet object has left the screen and remove from array

  7. 7

    Python. How to efficiently remove custom object from array

  8. 8

    Javascript - count and remove from an object

  9. 9

    Retrieval of title from json from indexed object array ios using swiftyjson

  10. 10

    Remove array key from multidimensional array

  11. 11

    Remove or Find an object with Mongoose when it is in an array by the object property value

  12. 12

    How to remove an array element in json-c?

  13. 13

    How to remove an array element in json-c?

  14. 14

    How to remove object with specific name from ArrayList

  15. 15

    How to remove single object from the sessionStorage (AngularJs)

  16. 16

    ngResource remove record from query object with $delete

  17. 17

    I cannot pull data from JSON object- array after ajax post?

  18. 18

    Keep getting [object, Object] from array for $.each

  19. 19

    How to remove a row from the javascript object based on a field and realign the object

  20. 20

    Remove duplicate hashes from two array in js?

  21. 21

    Remove whitespace from all array elements

  22. 22

    how to remove file details from multi array

  23. 23

    Remove Last Comma From Array Value in Loop

  24. 24

    How do I remove values from an array?

  25. 25

    How to remove \0 from the array of strings

  26. 26

    Remove the item from array in javascript titanium

  27. 27

    Remove value from array in magento field

  28. 28

    Imploding in PHP an array from Json

  29. 29

    Parsing JSON Object inside Array in Jackson

뜨겁다태그

보관