Comparing 2 JSONArray

北城以北 提交于 2021-02-18 19:27:28

问题


I have JSONArrays that needs to be compared which may have child entities inside it not in the same order though like this :

[{ "A" : "IN", "B" : "DL"},{ "A" : "US", "B" : "KA"}] //JSONArray 1
[{ "A" : "US", "B" : "KA"},{ "A" : "IN", "B" : "DL"}] //JSONArray 2

Here is my code. Before calling JsonElemnt, I am converting both the JSONArray to String and then passing it to my function to compare :

          //Converting both JSONArray to String JsonArray1Str and 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 are not null
    if (jsonElement1 != null && jsonElement2 != null) {

      // Check whether both jsonElement are 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 with Key values
          for (Entry<String, JsonElement> en : ens1) {
            isEqual = isEqual && compareJson(en.getValue(), json2obj.get(en.getKey()));
          }
        } else {
          return false;
        }
      }

      // Check whether both jsonElement are arrays
      else if (jsonElement1.isJsonArray() && jsonElement2.isJsonArray()) {
        JsonArray jarr1 = jsonElement1.getAsJsonArray();
        JsonArray jarr2 = jsonElement2.getAsJsonArray();
        if (jarr1.size() != jarr2.size()) {
          return false;
        } else {
          int i = 0;
          // Iterate JSON Array to JSON Elements
          for (JsonElement je : jarr1) {
            isEqual = isEqual && compareJson(je, jarr2.get(i));
            i++;
          }
        }
      }

      // Check whether both jsonElement are null
      else if (jsonElement1.isJsonNull() && jsonElement2.isJsonNull()) {
        return true;
      }

      // Check whether both jsonElement are 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, I am unable to find where I am going wrong. Could somebody please help me out ?

Edit: Any other approach to compare those two JSONArray are also welcomed.


回答1:


When both the objects are JSON Arrays, you are just comparing according to position of JSON objects, please find below code for correct at 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 to JSON Elements
      for (JsonElement je1 : jarr1) {
        boolean flag = false;
        for(JsonElement je2 : jarr2){
         flag = compareJson(je1, je2);
         if(flag){
          jarr2.remove(je2);
          break; 
         }
        }
        isEqual = isEqual && flag;
      }
    }
  }


来源:https://stackoverflow.com/questions/46209543/comparing-2-jsonarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!