java - Comparing 2 JSONArray -


i have jsonarrays needs compared may have child entities inside not in same order though :

[{ "a" : "in", "b" : "dl"},{ "a" : "us", "b" : "ka"}] //jsonarray 1 [{ "a" : "us", "b" : "ka"},{ "a" : "in", "b" : "dl"}] //jsonarray 2 

here code. before calling jsonelemnt, converting both jsonarray string , passing function compare :

          //converting both jsonarray string jsonarray1str , jsonarray2str           jsonelement jsonelement1 = parser.parse(jsonarray1str);           jsonelement jsonelement2 = parser.parse(jsonarray2str);           system.out.println(comparejson(jsonelement1, jsonelement2)); 

//comparejson function

  public static boolean comparejson(jsonelement jsonelement1, jsonelement jsonelement2) {     boolean isequal = true;     // check whether both jsonelement not null     if (jsonelement1 != null && jsonelement2 != null) {        // check whether both jsonelement objects       if (jsonelement1.isjsonobject() && jsonelement2.isjsonobject()) {         set<entry<string, jsonelement>> ens1 = ((jsonobject) jsonelement1).entryset();         set<entry<string, jsonelement>> ens2 = ((jsonobject) jsonelement2).entryset();         jsonobject json2obj = (jsonobject) jsonelement2;         if (ens1 != null && ens2 != null && (ens2.size() == ens1.size())) {           // iterate json elements key values           (entry<string, jsonelement> en : ens1) {             isequal = isequal && comparejson(en.getvalue(), json2obj.get(en.getkey()));           }         } else {           return false;         }       }        // check whether both jsonelement arrays       else if (jsonelement1.isjsonarray() && jsonelement2.isjsonarray()) {         jsonarray jarr1 = jsonelement1.getasjsonarray();         jsonarray jarr2 = jsonelement2.getasjsonarray();         if (jarr1.size() != jarr2.size()) {           return false;         } else {           int = 0;           // iterate json array json elements           (jsonelement je : jarr1) {             isequal = isequal && comparejson(je, jarr2.get(i));             i++;           }         }       }        // check whether both jsonelement null       else if (jsonelement1.isjsonnull() && jsonelement2.isjsonnull()) {         return true;       }        // check whether both jsonelement primitives       else if (jsonelement1.isjsonprimitive() && jsonelement2.isjsonprimitive()) {         if (jsonelement1.equals(jsonelement2)) {           return true;         } else {           return false;         }       } else {         return false;       }     } else if (jsonelement1 == null && jsonelement2 == null) {       return true;     } else {       return false;     }     return isequal;    } 

but, unable find going wrong. please me out ?

edit: other approach compare 2 jsonarray welcomed.

when both objects json arrays, comparing according position of json objects, please find below code correct @ code,

else if (jsonelement1.isjsonarray() && jsonelement2.isjsonarray()) {     jsonarray jarr1 = jsonelement1.getasjsonarray();     jsonarray jarr2 = jsonelement2.getasjsonarray();     if (jarr1.size() != jarr2.size()) {       return false;     } else {       // iterate json array json elements       (jsonelement je1 : jarr1) {         boolean flag = false;         for(jsonelement je2 : jarr2){          flag = comparejson(je1, je2);          if(flag){           jarr2.remove(je2);           break;           }         }         isequal = isequal && flag;       }     }   } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -