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

标签 json actionscript-3

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

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

例如:

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

是不同的字符串:

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

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

最佳答案

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中完成。

关于json - 在 AS3 中,如何检查两个 JSON 对象是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816008/

相关文章:

json - 如何使用对象作为 json 的键序列化/反序列化 ruby​​ 哈希/结构

apache-flex - 如何使用 flex4 嵌入纯 as3 位图 Assets (使用 flex3)

flash - 可以返回两种类型的方法的 AS3 最佳实践

actionscript-3 - Adobe Flash Builder 能否用于调试和分析 OpenLaszlo SWF10/SWF11 应用程序?

flash - 如何在 AS3 中播放库中的声音?

java - 编写大 JSON 文件避免内存不足问题的最佳方法

javascript - 以原始顺序迭代 jQuery JSON 对象

python - protobuf MessageToJson 删除值为 0 的字段

java - Retrofit2 和 converter-gson : Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

actionscript-3 - 在舞台周围绘制边框的最简单方法?