7

How to clear the data from a JSONArray

there is nothing like:

jsonArray.clear();

not even:

jsonArray.remove(index);

which was suggested here:

How do I remove a specific element from a JSONArray?

Thank you

Community
  • 1
  • 1
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226

9 Answers9

17
jsonArray = new JSONArray(new ArrayList<String>());
Carnal
  • 21,744
  • 6
  • 60
  • 75
  • 5
    You seem to have a confident answer, but I'm curious - will this lead to an unused JSONArray copy after every new? Similar to setting new values for a String variable? – Chucky May 30 '14 at 15:52
  • 1
    This is like suggesting to clear a List object by doing list = new ArrayList<>(); – Asu Dec 18 '18 at 21:37
  • this is obviously a mistake, this doesn't clear the array, it creates a new one so the previous one is not cleared at all, and if a reference to this object is kept on another variable, you'll be in big trouble. – Elad Lavi May 07 '23 at 12:20
7

this is not a perfect answer but you can do like this,

jsonArray=new jsonArray("[{}]");

EDIT

As per suggestion in comment, you can also use below code:

jsonArray=new jsonArray();
dd619
  • 5,910
  • 8
  • 35
  • 60
1

Just made Local Variables

JSONArray jsonArray = new JSONArray();
jsonArray.put(productlist);

Every time jsonserver data add using local block if u declear global block then need for clear jsonarray

Pang
  • 9,564
  • 146
  • 81
  • 122
1

I don't think "new JSONArray()" is a good solution.

Sometimes you will need to access the JSONArray object from within an inner class,so you have to declare the JSONArray object as final, and then you can't assign it to a new one anymore.

The best way is to remove its elements by loop.

while(myjsonarray.length()>0)
{
    myjsonarray.remove(0);
}

You said "there is nothing like jsonArray.remove(index);", and also tagged this question with "java", "android"

But now I am using Android Studio 3.6.3, and JSONArray class has this method, and you can find it in Android Studio official documents as well. Therefore, I guess you should edit the title of the question like "why my android studio doesn't has the full methods of JSONArray class"

vainquit
  • 491
  • 6
  • 13
0

I try everything but the only thing that worked for me was

jsonArray.clear();

or you can make it local variable again

JSONArray jsonArray = new JSONArray();
0

In the library I have, there doesn't seem to be any way to call clear() of the private ArrayList inside of the JSONObject. It does however have public Object remove(int index). I am just using new JSONObject() :(

Brandon Lockaby
  • 646
  • 5
  • 12
0

jsonArray.clear(); This worked for me. I was using JSONArray from org.json (not org.json.simple) library.

0

You need to run a while loop across the length of the JSONArray and at each time delete the first index. the loop will reach a stage when the length of the JSONArray is zero to show the JSONArray is not empty and and cleared.

while (optionArray.length() > 0) {
            optionArray.remove(0)
    }
-4

this is a one answer ,please try it:

jsonArray=[]
Jhanvi
  • 5,069
  • 8
  • 32
  • 41