【问题标题】:Comparing 2 JSONArray比较 2 JSONArray
【发布时间】:2017-09-14 02:27:02
【问题描述】:

我有需要比较的 JSONArrays,其中可能有子实体,但顺序不同:

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

这是我的代码。 在调用 JsonElemnt 之前,我将 JSONArray 都转换为 String,然后将其传递给我的函数进行比较:

          //Converting both JSONArray to String JsonArray1Str and JsonArray2Str
          JsonElement jsonElement1 = parser.parse(JsonArray1Str);
          JsonElement jsonElement2 = parser.parse(JsonArray2Str);
          System.out.println(compareJson(jsonElement1, jsonElement2));

//比较Json函数

  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;

  }

但是,我找不到哪里出错了。有人可以帮帮我吗?

编辑:也欢迎任何其他方法来比较这两个 JSONArray。

【问题讨论】:

  • 你试过通过打印出数组来调试它吗?
  • 您可以通过使用 GSON 将 json 转换为 java 对象,然后使用比较器接口进行比较。

标签: java arrays json jackson


【解决方案1】:

当两个对象都是JSON数组时,你只是根据JSON对象的位置进行比较,请在代码中找到下面的代码,

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;
      }
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多