1

整数でいっぱいの 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;

}

基本的に、両方の JSONArray が同じ長さかどうかを確認します。そうであれば、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);
 }   

これは、非常に多くのコードのように思えます。これを行うためのより簡単な方法があるかどうか誰かが知っていますか?

4

1 に答える 1

0

配列を変換してから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;
}
于 2015-10-30T03:10:36.820 に答える