I have two JSONObjects with the same structure, but I want to combine them so that the characteristics are kept in common and that those that are different give priority to the second object.
Example:
JSONObject obj1 = new JSONObject("{
"name":"manu",
"age":23,
"occupation":"SE"
}")
JSONObject obj2 = new JSONObject("{
"name":"manu",
"age":23,
"country":"india",
"email" : "[email protected]"
}")
Expected:
JSONObject result = {
"name":"manu",
"age":23,
"occupation":"SE",
"country":"india",
"email" : "[email protected]"
}
I tried the following:
JSONObject obj1 = new JSONObject("{\n"
+ "\"name\":\"manu\",\n"
+ "\"age\":23,\n"
+ "\"occupation\":\"SE\"\n"
+ "}");
JSONObject obj2 = new JSONObject("{\n"
+ "\"name\":\"manu\",\n"
+ "\"age\":23,\n"
+ "\"country\":\"india\"\n"
+ "}");
JSONObject result = new JSONObject(obj2.toString());
Iterator<?> keys = obj1.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!result.has(key))
result.put(key, obj1.get(key));
}
But I do not consider it efficient, because I will use it for very large Jsons