0

I have 2 jsons like {"a1":{"b1":{"c1":"value1"},"b2":{"c2":{"d2":"value2"}}}} and {"a1":{"b2":{"c2":{"d2":"value2"}},"b1":{"c1":"value1"}}}. I am trying to write a method that compares the 2 jsons. I have a recursive function, but I don't know how to check whether a json object is nested with other json objects or not (this is required for putting an end criteria in recursion)

Here's my code so far:

 public static boolean verifyResponse(JSONObject json1,JSONObject json2)
    {
        Object keyarray[]=json1.keySet().toArray();
        int i=0;
        while(i<keyarray.length)
        {
            if(!verifyResponse((JSONObject)json1.get(keyarray[i].toString()),(JSONObject)json2.get(keyarray[i].toString())))
            {
                return false;
            }
            i++;
        }
        return true;
    } 
Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • Related, overlapping and possible duplicate of: http://stackoverflow.com/questions/6118708/determine-whether-json-is-a-jsonobject-or-jsonarray – Danny Varod Aug 07 '15 at 10:50
  • Also related: http://stackoverflow.com/questions/18592762/gson-identifying-json-object-as-primitive – Danny Varod Aug 07 '15 at 10:52
  • Here I don't know the key names as well. – Lakshit Pande Aug 07 '15 at 10:55
  • You have three cases here, simple value, object {} and array [], where the array can contain any of the three cases as an item. You could trim the surrounding whitespace, then check the first and last characters. – Danny Varod Aug 07 '15 at 12:05

0 Answers0