在AS3中,如何检查两个JSON对象是否相等?

jus*_*vin 3 json actionscript-3

标题几乎说了.我有两个JSON对象,我想知道它们是否相等(具有所有相同的属性值).

我可以对它们进行字符串化,但我不确定两个相等的对象是否总能产生相同的输出:

例如:

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "favoriteColors": ["blue", "green", "red"]
}
Run Code Online (Sandbox Code Playgroud)

是一个不同的字符串:

{
    "age": 25,
    "lastName": "Smith",
    "firstName": "John",
    "favoriteColors": ["blue", "green", "red"]
}
Run Code Online (Sandbox Code Playgroud)

但作为对象,它们具有相同的属性.

Bar*_*klı 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;
Run Code Online (Sandbox Code Playgroud)

如果您不想要整个SDK,也许您可​​以获得此功能/类并使用该源.

你可以在这里看到来源.大多数工作都是在函数中完成的internalCompare.