Skip to content

Instantly share code, notes, and snippets.

@baladkb
Created January 7, 2017 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baladkb/d599e8e564c54eabd8df01f7a54f521c to your computer and use it in GitHub Desktop.
Save baladkb/d599e8e564c54eabd8df01f7a54f521c to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class JSONCompare {
HashMap<String, Object> data;
public static void main(String[] args) throws JSONException {
JSONObject json1_new = new org.json.JSONObject(
"{\"2/1/2017\":\"100\",\"1/1/2016\":\"200\"}");
JSONObject json1_old = new org.json.JSONObject(
"{\"2/1/2017\":\"1\",\"1/1/2016\":\"1\",\"1/2/2016\":\"1\"}");
JSONCompare obj = new JSONCompare();
System.out.println(obj.updateNewToOldData(json1_old, json1_new));
}
public JSONObject updateNewToOldData(JSONObject oldJson, JSONObject newJson)
throws JSONException {
data = new HashMap<String, Object>();
JSONObject json1_new = new JSONObject();
try {
Iterator<String> keysIter = newJson.keys();
while (keysIter.hasNext()) {
String key = keysIter.next();
Object value = newJson.get(key);
data.put(key, value);
}
for (String key : data.keySet()) {
if (oldJson.has(key)) {
json1_new.put(key, data.get(key));
}
}
} catch (JSONException ex) {
}
return json1_new;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment