2013-04-04 120 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); 
} 
相关问题