比较两个JSON数组

7

我有一个间隔,它执行ajax请求,目前是每5秒。我遇到了if语句的问题。我的代码总是进入它,而且两个json值完全相同,为什么它会将它们视为不同?

var newActivity = null, oldActivity = null;
setInterval(function(){
    $.ajax({
        type: "get",
        url: "/get/new_activity",
        dataType: "json",
        success: function(data){
            oldActivity = newActivity;
            newActivity = data;
            console.log(JSON.stringify(oldActivity));
            console.log(JSON.stringify(newActivity));
            if(JSON.stringify(oldActivity) != JSON.stringify(newActivity)){
                $("#new-activity").slideDown( "fast" );
            }
        }
    });
}, 5000);

编辑
以下是控制台输出(虚线仅用于分隔请求,不在实际输出中)

null
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

---------------------------------------------------

[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

---------------------------------------------------

[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]
[{"title":"How many planets are in the solar system?","title_url":"How-many-planets-are-in-the-solar-system%3F","id":"2","answers":"1","asked":"2013-01-11 10:03:50","asked_pretty":"Today","activity":"2013-01-11 12:33:53","activity_pretty":"Today"},{"title":"Why is the sky blue?","title_url":"Why-is-the-sky-blue%3F","id":"1","answers":"1","asked":"2013-01-11 09:55:13","asked_pretty":"Today","activity":"2013-01-11 12:03:45","activity_pretty":"Today"}]

1
请发布newActivityoldActivity的JSON字符串化所得到的实际值。如果它们包含对象,则在JSON格式中没有排序或相等性的保证。(还有其他等效但不完全相同的编码方式;它们较少见。) - user166390
1
@Christophe,但是第一个输出列表显示它根本不正确 - user166390
1
@Christophe pst 是正确的,我删除了我的答案,因为它是错误的。 - bfavaretto
2
@Christophe:他的代码表明它们是不同的事实。 - gen_Eric
2
我是个白痴。我在另一个函数中将newActivity重置为null。这就是为什么它们每次都返回不相等的原因。 - Get Off My Lawn
显示剩余13条评论
3个回答

10

JSON对象的序列化方式不能保证相同,其属性顺序也不能保证相同。使用JSON.stringify函数测试对象相等性并不是一个好的方法。

一个更好的示例是下面这个函数(在一段时间前在互联网上找到的,但很遗憾我无法给原作者留下功劳)。

/**
 * Deep compare of two objects.
 *
 * Note that this does not detect cyclical objects as it should.
 * Need to implement that when this is used in a more general case. It's currently only used
 * in a place that guarantees no cyclical structures.
 *
 * @param {*} x
 * @param {*} y
 * @return {Boolean} Whether the two objects are equivalent, that is,
 *         every property in x is equal to every property in y recursively. Primitives
 *         must be strictly equal, that is "1" and 1, null an undefined and similar objects
 *         are considered different
 */
function equals ( x, y ) {
    // If both x and y are null or undefined and exactly the same
    if ( x === y ) {
        return true;
    }

    // If they are not strictly equal, they both need to be Objects
    if ( ! ( x instanceof Object ) || ! ( y instanceof Object ) ) {
        return false;
    }

    // They must have the exact same prototype chain, the closest we can do is
    // test the constructor.
    if ( x.constructor !== y.constructor ) {
        return false;
    }

    for ( var p in x ) {
        // Inherited properties were tested using x.constructor === y.constructor
        if ( x.hasOwnProperty( p ) ) {
            // Allows comparing x[ p ] and y[ p ] when set to undefined
            if ( ! y.hasOwnProperty( p ) ) {
                return false;
            }

            // If they have the same strict value or identity then they are equal
            if ( x[ p ] === y[ p ] ) {
                continue;
            }

            // Numbers, Strings, Functions, Booleans must be strictly equal
            if ( typeof( x[ p ] ) !== "object" ) {
                return false;
            }

            // Objects and Arrays must be tested recursively
            if ( !equals( x[ p ],  y[ p ] ) ) {
                return false;
            }
        }
    }

    for ( p in y ) {
        // allows x[ p ] to be set to undefined
        if ( y.hasOwnProperty( p ) && ! x.hasOwnProperty( p ) ) {
            return false;
        }
    }
    return true;
},

我在这个答案中写了一个更简单的解决方案,但是思路相同:https://dev59.com/UGbWa4cB1Zd3GeqPW3f9#33378418 - Price

0
一个解决方案是将json数据作为文本读取,成功后检查服务器响应字符串是否与上次响应字符串不同。然后您可以对该字符串调用JSON.parse。但这使得比较更容易,并且只需要稍微修改您的代码即可。

1
但是dataType被设置为“json”,这意味着在您的情况下数据是一个对象,这使得比较困难。我的建议是将dataType设置为“text”,然后在比较时跳过JSON.stringify。 - Strille

-2

I think you should try comparing their properties.


<code>for(var property in JSON.stringify(oldActivity))
    {
      if(JSON.stringify(oldActivity)[property]!=JSON.stringify(newActivity)[property])
                   $("#new-activity").slideDown( "fast" );
    }</code>
    


3
如果你要比较属性,请不要使用 JSON.stringify - gen_Eric
2
你只是把 stringify 问题推迟了一层,需要使用递归函数,否则你将不得不为每个 JSON 结构的每个级别实现自定义测试。 - Ruan Mendes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接