I am trying to copy one json say json1 to another json say json2. It seems values get overwrited ie., if am putting value in one key say key1 of json1. key1 value of json2 get overwritted.
What I have tried:
Works :
JSONObject json1 = new JSONObject();
json1.put("a",new JSONObject().put("c",1)).put("b","2");
JSONObject json2 = new JSONObject();
Iterator itr = json1.keys();//copying json from source to destinationJson
while(itr.hasNext()) {
String key = (String) itr.next();
json2.put(key, json1.get(key));
}
json1.put("b",3);
out.print("json1"+json1);
out.print("json2"+json2);
here , value b didnot get overwrite .
but if put value in c. It get overwritted. seems a of two jsons share same memory.
JSONObject subjson = json1.getJSONObject("a");
subjson.put("a",2);
out.print("json1"+json1);
out.print("json2"+json2);
So how can i solve it ? :( ..