比较2个JSONArray [英] Comparing 2 JSONArray

查看:546
本文介绍了比较2个JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要比较的JSONArrays,尽管里面的子实体可能不一样,但是像这样:

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

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

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函数

//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 ?

欢迎使用其他任何方法来比较这两个JSONArray.

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

推荐答案

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

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

这篇关于比较2个JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆