How to clear data from a JSON array

I am working on a project where I have to clear all data from a JSON array. There seems to be no method like jsonArray.clear () . Also tried jsonArray = new JSONArray () . That didn't work either. Suggestions please

+8
java json android arrays
source share
5 answers

Just create a new JSONArray.

JSONArray otherJsonArray = new JSONArray(); 

Or iterate over an array and remove(int index) indexes.

http://www.json.org/javadoc/org/json/JSONArray.html#remove(int)

+8
source share

Just put jsonArray = new JSONArray()

+3
source share

And you use this otherJsonArray already exists, then you use

 JSONArray otherJsonArray = new JSONArray("[]"); 
+1
source share

Creating a new one will work if you did not pass it as a parameter to the method, in which case you need to change the reference object, since the new link will not be visible by the calling method.

So, if that is the case, do it in the opposite direction, this way you will not get your iterator exceeding the bounds:

  int startingLength = someJsonArray.length(); for (int i = startingLength - 1; i >= 0; i--) { someJsonArray.remove(i); } 
+1
source share

We can use someJsonArray.pop (index) to remove the required entry. We can use this code in a loop to delete all records.

0
source share

All Articles