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
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.
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? JsonObject::remove() removes a key-value pair from the object pointed by the JsonObject . If the JsonObject is null, this function does nothing.
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.
jsonArray = new JSONArray(new ArrayList<String>());
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With