Created
January 7, 2017 10:22
-
-
Save baladkb/d599e8e564c54eabd8df01f7a54f521c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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