10

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

1
  • 5
    why doesn't jsonArray = new JSONArray() work? Commented Feb 19, 2013 at 1:47

6 Answers 6

10

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)

Sign up to request clarification or add additional context in comments.

5 Comments

But there is no method remove(int) for JSONArray. My IDE is not showing that. I am using Android development tool(Eclipse)
No method remove on JSONArray? It's not static, so that's not surprising. It's an instance method, it would be jsonArray.remove(); unless you're not using the same org.json.JSONArray object?
Am using org.json.JSONArray object. I changed that object to static and still it don't have the method remove()
You've misunderstood me, the method should exist on the instance (jsonArray), not on the JSONArray class as a static method.
if you iterate incrementally to remove(index) , first time will be 0 and will delete lets say from A,B,C "A" after the second loop with 1 it will delete "C" and for the 3rd loop it will not delete nothing since there is no more things after C , so it will return B and will not delete all the array, you need to iterate decrementally to do it
4

Just put jsonArray = new JSONArray()

Comments

2

Creating a new one will work, unless you have passed it as a parameter to a method in which case you need to modify the referenced object as a new reference will not be seen by the calling method.

So if that is the case, do it backwards, that way you won't get your iterator exceeding bounds:

    int startingLength = someJsonArray.length();

    for (int i = startingLength - 1; i >= 0; i--) {

        someJsonArray.remove(i);

    }

Comments

1

And you use that otherJsonArray is already existing then you use

JSONArray otherJsonArray = new JSONArray("[]");

Comments

0

We can use someJsonArray.pop(index) to remove require record. We can use this code in loop to remove all records.

Comments

0

I have a situation where I want to remove all the entries from a JSONArray with key "Constants", which is an element in a JSONObject, creating a new JSONArray an assigning it does NOT clear the JSONArray, I have to iterate through the JSONArray and jsonArray.remove(i) on each of them, but there is a second method that works which involves removing the array element, in this case "Constants" completely from the JSONObject and re-adding it as a new JSONArray.

Hereis code with assignment of new array, which does not work, the JSONArray remained unchanged: (I tried both above suggestions for new JSONArray(); and new JSONArray("[]");

        JSONObject jsonObj = new JSONObject(metadataOriginalJSON);
        if (jsonObj.isJSONArray("Constants")) {
            JSONArray constantsArray = jsonObj.getJSONArray("Constants");
            constantsArray = new JSONArray();
            metadataConstantsRemoved = jsonObj.toString();
        }

Here is code for the iteration through the JSONArray which worked:

  JSONObject jsonObj = new JSONObject(metadataOriginalJSON);
  if (jsonObj.isJSONArray("Constants")) {
      JSONArray constantsArray = jsonObj.getJSONArray("Constants");
      int i = 0;
      int arrayLenSanityCheckPreventEndlessLoop = constantsArray.length();
      while (constantsArray.length() > 0 && i < arrayLenSanityCheckPreventEndlessLoop) {
          constantsArray.remove(0);
          i++;
      }
      metadataConstantsRemoved = jsonObj.toString();
  }

The 2nd method that works by removing the entire JSONArray element and re-adding it to the JSONObject:

  JSONObject jsonObj = new JSONObject(metadataOriginalJSON);
  if (jsonObj.isJSONArray("Constants")) {
      jsonObj.remove("Constants");
      jsonObj.put("Constants", new JSONArray());
      metadataConstantsRemoved = jsonObj.toString();
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.