'Is there a way, I can empty the whole JSONObject -- java
I have to populate a json object like this, let say it is named detailJSON:
{"amount": "5.00", "ac_no": "123456" }
I do it this way:
detailJSON.put("amount","5.00");
detailJSON.put("ac_no","123456");
After this, the detail is entered in some shared preferences, and now I want to clear this JSONObject and use the same detailJSON object to store another json (with different keys), this way:
{"amount":"6.00", "loan_no":"123456"}
I know there is a method, remove(), that removes the particular key and corresponding value.
This works:
detailJSON.remove("amount");
detailJSON.remove("ac_no");
and then use it --
detailJSON.put("amount","6.0");
detailJSON.put("loan_no","123456");
Now this is a very simple example. In the code I'm working on, I have a lot of keys, so using remove actually increases the LOC. Also, each time before removing I need to check whether JSONObject has
that particular key or not.
Is there any other way, I can implement clearing of the JSONObject??
I tried
detailJSON=null ;
detailJSON=new JSONObject();
But it does not work.
I am basically in search of something like clear() method, if exists.
Solution 1:[1]
Iterator keys = detailJSON.keys();
while(keys.hasNext())
detailJSON.remove((String)detailJSON.keys().next());
Solution 2:[2]
this version has an overhead because of getting the key-set everytime, but it works without concurrent modification exception.
while(json.length()>0)
json.remove(json.keys().next());
Solution 3:[3]
You could, but it will be a hack.
Iterator i = detailJSON.keys();
while(i.hasNext()) {
i.next().remove();
}
//or
detailJSON.keySet().clear();
It works, because JSONObject.keySet()
will return you this.map.keySet()
. And what JavaDoc for HashMap.keySet()
said:
Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.
Java's HashMap
from collections will return you java.util.HashMap.KeySet
and java.util.HashMap.KeySet.clear();
just calls map.clear()
(KeySet is an inner class of java.util.HashMap)
Solution 4:[4]
you can use this method
public HashMap clearMap(HashMap detailJSON){
for(String key: detailJSON.keySet())
detailJSON.remove(key);
return detailJSON;
}
Solution 5:[5]
if detailJSON
is an map variable, the you can use detailJSON.clear()
method to empty values in the map.
Solution 6:[6]
It appears there is no clear method for JSONObject
:. if you have to iterate all the JSONObject
to clear it, better you get the parent node, and update with a new JSONObject()
.
You probably using while
to get those details, so you can do this:
Iterator keys = mJson.keys();
while(keys.hasNext())
{
String key = keys.next();
JSONObject detailJSON = mJson.getJSONObject(key);
JSONObject newDetail = new JSONObject();
...
mJson.put(key,newDetail);
}
And when using JSONArray
:
for (int i=0; i < mJson.length(); i++)
{
JSONObject detailJSON = mJson.getJSONObject(i);
JSONObject newDetail = new JSONObject();
...
mJson.put(i,newDetail);
}
Solution 7:[7]
Make the JSON a string and print it. Like:
JSONObject jo =new JSONObject();
System.out.println(jo.toString()); ```
Solution 8:[8]
ConcurrentModificationException Proof ?
Directly using key set iterator of a JSONObject
inside a loop for removing items will cause throw ConcurrentModificationException.
This is because when you remove an element, the JSONObject
automatically modifies its key set that you are using inside the loop at the moment. And this's a violation!
One solution is to copy keys to an array first, then use the array instead of the iterator itself, like below :
public static void clearJson(JSONObject json) {
if (json.length() > 0) {
String[] keysArr = new String[json.length()];
int counter = 0;
for (Iterator<String> iter = json.keys();iter.hasNext();)
keysArr[counter++] = iter.next();
for (String key : keysArr)
json.remove(key);
}
}
Solution 9:[9]
you could re-declare the variable
JSONObject detailJSON= new JSONObject();
detailJSON.put("amount","5.00");
detailJSON.put("ac_no","123456");
detailJSON = new JSONObject()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow