Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

good way to clone JSONObject on Android

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?

like image 405
ligi Avatar asked Nov 01 '22 22:11

ligi


1 Answers

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

like image 141
Ali Avatar answered Nov 12 '22 12:11

Ali