Is there a direct way to remove an JSONObject stored in the JSONArray by using index. I tried all the possibilities. Still not able to remove the JSON object from the JSON Array. Any hint will be helpful Thanks
In java-json , there is no direct method to remove jsonObject, but using json-simple , it is simple to do so:
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject.put("key1", "value1");
jsonObject1.put("key2", "value2");
jsonObject2.put("key3", "value3");
jsonArray.add(jsonObject);
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
//........ Whole Json Array
System.out.println(jsonArray);
//To remove 2nd jsonObject (index starts from 0)
jsonArray.remove(1);
// Now the array will not have 2nd Object
System.out.println(jsonArray);
Have you tried using delete to do this?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
just get the index of the JSON object in the json array
and remove the json object by array.splice(index,howmany,item1,.....,itemX) method
for more information just use this link http://www.w3schools.com/jsref/jsref_splice.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.