Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a JSONArray

Tags:

java

json

android

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

like image 891
Archie.bpgc Avatar asked Dec 11 '12 11:12

Archie.bpgc


People also ask

How do I remove an object from JSONArray?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

Can JSONArray be null?

Null valuesJSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.

How do you remove a key value pair from a JSON object?

How do you remove a key value pair from a JSON object? JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.

What is the use of JSONArray?

JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.


3 Answers

jsonArray = new JSONArray(new ArrayList<String>());
like image 58
Carnal Avatar answered Sep 27 '22 19:09

Carnal


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();
like image 36
dd619 Avatar answered Sep 27 '22 17:09

dd619


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

like image 30
chandrakant Chaturvedi Avatar answered Sep 27 '22 17:09

chandrakant Chaturvedi