【问题标题】:compare org.codehaus.jettison.json.JSONArray without regard to order比较 org.codehaus.jettison.json.JSONArray 不考虑顺序
【发布时间】:2015-10-30 00:32:04
【问题描述】:

我有 2 个完整的整数 JSONArray。

我想比较它们以获得相同的内容而不考虑顺序。

所以:

[1, 2] == [1, 2] 真 [1, 2] == [2, 1] 是的

JSONArray 有

public boolean equals(Object o) 

但对于 [1, 2] == [2, 1] 会返回 FALSE

所以,我推出了自己的:

public boolean isEqual(JSONArray inputJsonArray, 
                       JSONArray outputJsonArray) throws JSONException{
    boolean equal=true, done;
    int idx = 0;      

    if (inputJsonArray.length() == outputJsonArray.length()){

        //make sure all elements in input array are in output array
        done=false;
        while (!done){
            if(idx >= inputJsonArray.length()){
               done=true;
            }
            else if (isIntInJsonArray(outputJsonArray,
                                      inputJsonArray.getInt(idx)) == false){
                     equal = false;
                     done=true;
            }
            else{
                     idx ++;
            }

        }

        if (equal){

            //make sure all elements in output array are in input array
            done=false;
            while (!done){
                  if (idx >= outputJsonArray.length()){
                       done=true;
                  }
                  else if (isIntInJsonArray(inputJsonArray,
                                         outputJsonArray.getInt(idx)) == false){

                            equal = false;
                            done=true;
                  }
                  else{
                            idx++;
                  }

            }          
        }           
    }
    else{
            equal = false;
    }

    return equal;

}

基本上,我检查两个 JSONArrays 的长度是否相同。如果是,那么我确保 outputJsonArray 中的每个元素都在 inputJsonArray 中,反之亦然。执行此操作的主力方法是:

private boolean isIntInJsonArray(JSONArray inputJsonArray, int mInt) throws JSONException{
    boolean found=false, done=false;
    int idx = 0;

    while (!done){
            if(idx >= inputJsonArray.length()){
               done=true;
            }
            else if (inputJsonArray.getInt(idx) == mInt){
               found = true;
               done=true;
            }
            else{
               idx ++;
            }              
    }

    return(found);
 }   

这让我印象深刻,就像一大堆代码一样。有谁知道是否有更简单的方法可以做到这一点?

【问题讨论】:

    标签: java json spring jettison


    【解决方案1】:

    将数组转换为JSONObject,然后使用其equals 方法。

    JSONArray arr1 = new JSONArray();
    JSONArray arr2 = new JSONArray();
    
    arr1.put(1);
    arr1.put(2);
    arr1.put(3);
    arr1.put(4);
    arr1.put(5);
    
    arr2.put(2);
    arr2.put(1);
    arr2.put(3);
    arr2.put(5);
    arr2.put(4);
    
    JSONObject o1 = arr1.toJSONObject(arr1);
    JSONObject o2 = arr2.toJSONObject(arr2);
    
    System.out.println(o1.equals(o2)); //true
    

    查看JSONObject的源代码,它正在使用它的底层映射来检查相等性。

    @Override
    public boolean equals(Object obj) {
       if (obj instanceof JSONObject) {
          return myHashMap.equals(((JSONObject)obj).myHashMap);
       } else {
          return false;
       }
    }
    

    底层地图的equals实现不考虑其内容的顺序

    public boolean equals(Object o) {
        if (o == this)
            return true;
    
        if (!(o instanceof Map))
            return false;
        Map<K,V> m = (Map<K,V>) o;
        if (m.size() != size())
            return false;
    
        try {
            Iterator<Entry<K,V>> i = entrySet().iterator();
            while (i.hasNext()) {
                Entry<K,V> e = i.next();
                K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(m.get(key)==null && m.containsKey(key)))
                        return false;
                } else {
                    if (!value.equals(m.get(key)))
                        return false;
                }
            }
        } catch (ClassCastException unused) {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
    
        return true;
    }
    

    【讨论】:

    • 这不适用于具有内部数组和对象的 codehaus 抛弃 JSONArrays 和 JSONObjects。在 AWS 库中的 org.json 更改后,我最终使用 wink apache 项目轻松比较对象。如何使用 codehaus jettison 库比较深层 JSON 对象?
    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2016-12-25
    • 2019-10-26
    • 1970-01-01
    • 2010-10-25
    • 2019-12-06
    • 2014-06-30
    • 2017-11-10
    相关资源
    最近更新 更多