Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear data from a JSON Array

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

like image 371
Joseph Avatar asked Feb 19 '13 01:02

Joseph


People also ask

How do you clear an array in JSON?

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.

How do you clear a JSON array in Python?

To delete a JSON object from a list: Parse the JSON object into a Python list of dictionaries. Use the enumerate() function to iterate over the iterate over the list. Check if each dictionary is the one you want to remove and use the pop() method to remove the matching dict.

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

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

Does JSON support null?

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


2 Answers

Just create a new JSONArray.

JSONArray otherJsonArray = new JSONArray();

Or iterate through the array and remove(int index) the indexes.

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

like image 70
Anew Avatar answered Oct 23 '22 13:10

Anew


Just put jsonArray = new JSONArray()

like image 24
Joabe Lucena Avatar answered Oct 23 '22 14:10

Joabe Lucena