'Modifying JSONObject in java , getting exception of ConcurrentModificationException
I have to modify each value for key inside JSONObject and I'm using recursion to iterate over JSONObject because it can be nested. But I'm getting exception in it.next()
I tried to change approach with foreach loop, fori and foreach with remainingkeys but not able to resolved.
Iterator<String> it = myList.iterator();
while (it.hasNext()) {
String value = it.next();
jsonObject.put(value, newValue);
}
Complete Recursion method is something like this :
public void handleJSON(Object input, Map<String,Object> result ){
if (input instanceof JSONObject) {
for (String key: ((JSONObject) input).keySet()) {
if (!(((JSONObject) input).get(key) instanceof JSONArray))
if (((JSONObject) input).get(key) instanceof JSONObject) {
handleJSONObject(((JSONObject) input).get(key), map);
} else {
Object value = ((JSONObject) input).get(key);
((JSONObject) input).put(key, handleValue(value, map));
}
else
handleJSONObject(new JSONArray(((JSONObject) input).get(key).toString()), map);
}
}
if (input instanceof JSONArray) {
for (int i = 0; i < ((JSONArray) input).length(); i++) {
JSONObject jsonObject = ((JSONArray) input).getJSONObject(i);
handleJSONObject(jsonObject, map);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|