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
Asked
Active
Viewed 2.1k times
4
-
I need to accomplish task using JAVA. forget to add it in the question – Timothy Rajan Nov 07 '13 at 13:53
-
possible duplicate of [How do I remove a specific element from a JSONArray?](http://stackoverflow.com/questions/8820551/how-do-i-remove-a-specific-element-from-a-jsonarray) – C-- Jan 28 '14 at 03:31
3 Answers
6
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);

Jhanvi
- 5,069
- 8
- 32
- 41
-
6
-
API level refers to the Android SDK (which you do not need to care about if you are not programming for Android, as seems to be the case in this question) – personne3000 Sep 24 '14 at 05:44
0
Have you tried using delete to do this?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

R. Barzell
- 666
- 5
- 24
-
Thanks for that. I need to delete it using JAVA. Could you please help me with JAVA code. I tried all API's. Might be I am missing something. – Timothy Rajan Nov 07 '13 at 13:53
-
No. I am looking for a remove method or something similar. Not able to get it. If this doesnt work, I need to convert the JSONArray to a Normal array and remove. Lot of coding involved. Looking to avoid it. – Timothy Rajan Nov 07 '13 at 14:08
0
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

Ranjit Swain
- 309
- 2
- 12
-
1Thanks for this. I believe your solution holds good for JAVAscript. I am looking for a JAVA solution – Timothy Rajan Nov 07 '13 at 14:06