Search code examples
javajsonorg.json

How do I clone an org.json.JSONObject in Java?


Is there a way to clone an instance of org.json.JSONObject without stringifying it and reparsing the result?

A shallow copy would be acceptable.


Solution

  • 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));