In AS3, how to check if two JSON objects are equal? In AS3, how to check if two JSON objects are equal? json json

In AS3, how to check if two JSON objects are equal?


There is a method for this in the Flex SDK. It is in the class ObjectUtil. Here is the description from the source :

/**     * 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;

If you don't want the whole SDK maybe you can just get this function/class and use that source.

You can see the source here. Most of the work is done in the function internalCompare.


Edit: Barış' answer is the best option, as it's tried and tested. Just in case this comes in handy for someone though:

Given that JSON values are limited to a small set of simple types, it should be possible to recurse through the properties fairly easily. Something along these lines works with your example:

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;}


Maybe you can convert the two objects to string then compare them

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