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.
Therefore, all you need to do is turn the JSONObject into a String, and then back into a JSONObject. You could store the string as a variable (or use it as a return value), then simply take your preexisting JSONObject reference and use the method to rebuild the JSONObject from the String.
simple package contains important classes like JSONValue, JSONObject, JSONArray, JsonString and JsonNumber. We need to install the json-simple. jar file to execute a JSON program whereas org. json library has classes to parse JSON for Java.
Easiest (and incredibly slow and inefficient) way to do it
JSONObject clone = new JSONObject(original.toString());
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With