I have to clone a JSONObject on Android. I am aware of the easy way:
JSONObject clone = new JSONObject(original.toString());
but somehow it feels wrong/slow to do it this way. I found this: https://stackoverflow.com/a/12809884/322642 , but on Android I do not have JSONObject.getNames - anyone has a good pointer on how to do this?
the fastest + minimal way I found is this. it does deep copy.
JSONObject clone= new JSONObject(original.toMap());
Update: as per Marcos's comment the toMap() function is not available in Android. but the org.json library available on maven under groupId org.json has it: https://search.maven.org/artifact/org.json/json/20210307/bundle
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