Last active
January 7, 2017 07:49
-
-
Save baladkb/67477fb90f6ad607bc57b5c786efddbf to your computer and use it in GitHub Desktop.
This file contains 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 org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
import java.util.Map; | |
import java.util.Set; | |
public class JSONUtils { | |
public static boolean areEqual(Object ob1, Object ob2) throws JSONException { | |
Object obj1Converted = convertJsonElement(ob1); | |
Object obj2Converted = convertJsonElement(ob2); | |
return obj1Converted.equals(obj2Converted); | |
} | |
private static Object convertJsonElement(Object elem) throws JSONException { | |
if (elem instanceof JSONObject) { | |
JSONObject obj = (JSONObject) elem; | |
Iterator<String> keys = obj.keys(); | |
Map<String, Object> jsonMap = new HashMap<>(); | |
while (keys.hasNext()) { | |
String key = keys.next(); | |
jsonMap.put(key, convertJsonElement(obj.get(key))); | |
} | |
return jsonMap; | |
} else if (elem instanceof JSONArray) { | |
JSONArray arr = (JSONArray) elem; | |
Set<Object> jsonSet = new HashSet<>(); | |
for (int i = 0; i < arr.length(); i++) { | |
jsonSet.add(convertJsonElement(arr.get(i))); | |
} | |
return jsonSet; | |
} else { | |
return elem; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment