2013-04-04 112 views
3

標題說得很對。我有兩個JSON對象,我想知道它們是否相等(具有所有相同的屬性值)。在AS3中,如何檢查兩個JSON對象是否相等?

我可以字符串化他們兩個,但我不知道,如果兩個相等的對象總是會產生相同的輸出:

如:

{ 
    "firstName": "John", 
    "lastName": "Smith", 
    "age": 25, 
    "favoriteColors": ["blue", "green", "red"] 
} 

是從不同的字符串:

{ 
    "age": 25, 
    "lastName": "Smith", 
    "firstName": "John", 
    "favoriteColors": ["blue", "green", "red"] 
} 

但作爲對象他們有相同的屬性。

+0

你就不能檢查每個屬性對在彼此一個時間? '如果年齡=年齡,如果firstName = firstName等# – DasPete 2013-04-04 15:59:45

+0

我正在尋找一種適用於任何JSON對象的廣義解決方案。另外,favoriteColors是一個數組,所以簡單的相等比較將不起作用。 – justkevin 2013-04-04 16:09:55

+0

我的JSON有點生疏,但是你可以循環訪問第一個對象的屬性,然後爲每個對象檢查第二個對象的屬性,直到找到匹配的屬性。一旦你有兩個匹配的屬性,檢查它們的值是否相同。保持循環,直到所有屬性都被檢查。這也可以爲數組工作,只是檢查它是否是一個數組,然後輸入數組項的另一個循環。 – DasPete 2013-04-04 16:17:11

回答

8

在Flex SDK中有一個這樣的方法。它在類ObjectUtil。這是從源頭上描述:

/** 
    * Compares the Objects and returns an integer value 
    * indicating if the first item is less than greater than or equal to 
    * the second item. 
    * This method will recursively compare properties on nested objects and 
    * will return as soon as a non-zero result is found. 
    * By default this method will recurse to the deepest level of any property. 
    * To change the depth for comparison specify a non-negative value for 
    * the depth parameter. 
    * @param a Object. 
    * @param b Object. 
    * @param depth Indicates how many levels should be 
    * recursed when performing the comparison. 
    * Set this value to 0 for a shallow comparison of only the primitive 
    * representation of each property. 
    * For example: 
    * var a:Object = {name:"Bob", info:[1,2,3]}; 
    * var b:Object = {name:"Alice", info:[5,6,7]}; 
    * var c:int = ObjectUtil.compare(a, b, 0);In the above example the complex properties of a and 
    * b will be flattened by a call to toString() 
    * when doing the comparison. 
    * In this case the info property will be turned into a string 
    * when performing the comparison. 
    * @return Return 0 if a and b are null, NaN, or equal. 
    * Return 1 if a is null or greater than b. 
    * Return -1 if b is null or greater than a. 
    * @langversion 3.0 
    * @playerversion Flash 9 
    * @playerversion AIR 1.1 
    * @productversion Flex 3 
    */ 
    public static function compare (a:Object, b:Object, depth:int=-1) : int; 

如果你不希望整個SDK,也許你可以得到這個函數/類,並使用該源。

您可以看到源here。大部分工作都在功能internalCompare中完成。

+0

奇怪的是,這種方法似乎沒有出現在Adobe的在線文檔中。我對它的目的也有點困惑 - 它似乎是爲排序而設計的,但排序順序很難預測具有多個屬性的對象。 – justkevin 2013-04-04 16:44:21

+0

該函數用於比較兩個對象,它可以通過將它作爲比較函數進行排序來用於排序。不知道爲什麼它不顯示在Adobe的文檔可能是因爲它是Flex的一部分。但是您可以在不使用整個SDK或製作Flex應用程序的情況下使用該類。 – 2013-04-04 16:45:57

+1

@justkevin adobe文檔頁面上的過濾器可能未設置爲顯示sdk文檔 – 2013-04-04 17:22:48

0

編輯:Barış的答案是最好的選擇,因爲它的嘗試和測試。以防萬一,這對於某人來說很方便:

鑑於JSON值僅限於一小組簡單類型,應該可以輕鬆地遍歷屬性。這些方針的東西與你的示例工作:

private function areEqual(a:Object, b:Object):Boolean { 
    if (a === null || a is Number || a is Boolean || a is String) { 
     // Compare primitive values. 
     return a === b; 
    } else { 
     var p:*; 
     for (p in a) { 
      // Check if a and b have different values for p. 
      if (!areEqual(a[p], b[p])) { 
       return false; 
      } 
     } 
     for (p in b) { 
      // Check if b has a value which a does not. 
      if (!a[p]) { 
       return false; 
      } 
     } 
     return true; 
    } 
    return false; 
} 
1

也許你可以在這兩個對象轉換爲字符串,然後比較它們

function compareJSON(a:Object, b:Object):Boolean 
{ 
    return JSON.stringify(a)===JSON.stringify(b); 
}