How do I clone an org.json.JSONObject in Java?
Use the public JSONObject(JSONObject jo, java.lang.String[] names)
constructor and the public static java.lang.String[] getNames(JSONObject jo)
method.
JSONObject copy = new JSONObject(original, JSONObject.getNames(original));
Cause $JSONObject.getNames(original)
not accessible in android,you can do it with:
public JSONObject shallowCopy(JSONObject original) { JSONObject copy = new JSONObject(); for ( Iterator<String> iterator = original.keys(); iterator.hasNext(); ) { String key = iterator.next(); JSONObject value = original.optJSONObject(key); try { copy.put(key, value); } catch ( JSONException e ) { //TODO process exception } } return copy;}
But remember it is not deep copy.