查找 JSON 在 JSON 对象中更改

Find JSON is Changed in JSON Object

我正在工作,每秒从 URL 请求数据

exports.getFootballNotRunning = function (callback) {

request({
    method: 'GET',
    url: 'http://ufxyz.ufabet.com/_View/RMOdds1Gen.ashx?ot=t&sort=0&at=EU',
}, function (error, response, body) {
    body = body.replace(/'/g, '"');
    body = JSON.parse(body);
    var setoffootball = [];
    for (var i = 0; i < body[2].length; i++) {
        setoffootball[i] = {
            league: body[2][i][0][1],
            matches: []
        };
        for (var j = 0; j < body[2][i][1].length; j++) {
            setoffootball[i].matches.push({
                firstteam: body[2][i][1][j][21],
                secondteam: body[2][i][1][j][23],
                time: body[2][i][1][j][10],
                fulltime: {
                    hdp: body[2][i][1][j][19],
                    h: body[2][i][1][j][32],
                    a: body[2][i][1][j][33],
                    goal: body[2][i][1][j][38],
                    over: body[2][i][1][j][39],
                    under: body[2][i][1][j][40]
                },
                firsthalf: {
                    hdp: body[2][i][1][j][49],
                    h: body[2][i][1][j][53],
                    a: body[2][i][1][j][54],
                    goal: body[2][i][1][j][57],
                    over: body[2][i][1][j][58],
                    under: body[2][i][1][j][59]
                }
            });
        }

    }
    notrunningfootball = setoffootball;
    callback(setoffootball);

});

}

就像这样,每一秒我都想检查我收到的 json 数组 JSON 是否与前一个不同,然后我可以发送更改的 JSON通过 socket.io 并在客户端 table 中更新

var io = require('socket.io')();
io.on('connection', function (socket) {
    console.log('test');
    sendMatches();
});
io.listen(7000);

function sendMatches() {
    setTimeout(function () {
        FT.getAllMatches(function (run, notrun) {
            io.emit('running', run);
            io.emit('notrunning', notrun);
            sendMatches();
        })
    }, 1000);
}

JSON 示例,它是一个包含许多 json 对象的数组,示例中只有一个 json

[{"league":"ENGLISH PREMIER LEAGUE","matches":[{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.25,"h":-9.7,"a":9.3,"goal":2,"over":8.6,"under":-9.3},"firsthalf":{"hdp":0,"h":6.9,"a":-7.8,"goal":0.75,"over":8.5,"under":-9.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0,"h":6.7,"a":-7.1,"goal":2.25,"over":-8.4,"under":7.7},"firsthalf":{"hdp":0.25,"h":-6.8,"a":5.9,"goal":1,"over":-7.3,"under":6.4}},{"firstteam":"Brighton & Hove Albion","secondteam":"Stoke City","time":"04:00","fulltime":{"hdp":0.5,"h":-7.2,"a":6.8,"goal":1.75,"over":6.5,"under":-7.2},"firsthalf":{"hdp":0,"h":0,"a":0,"goal":0,"over":0,"under":0}}]}]

流程是这样的 运行 app.js -> 服务器端:请求数据 -> 服务器端:将全部数据发送给客户端 -> 客户端:将所有数据提取到 table -> 接下来的 1 秒 -> 服务器端:请求数据->服务器端:比较旧数据和新数据,找到变化的json->服务器端:将与上一个不同的json发送给客户端->客户端:更新新 json 到 table

我想我没听懂你的意思.. 如果你只是想检查是否有差异,你可以简单地比较刺痛吗?..

JSON.stringify(a) === JSON.stringify(b)

这会告诉您是否有任何变化。 要找到差异,您可以进行字符串操作或使用下划线库 (_) 来帮助您。

var o1 = {a: 1, b: 2, c: 2},
    o2 = {a: 2, b: 1, c: 2};

_.omit(o1, function(v,k) { return o2[k] === v; })

在 o1 中对应但在 o2 中具有不同值的部分结果:

{a: 1, b: 2}

深度差异会有所不同:

function diff(a,b) {
    var r = {};
    _.each(a, function(v,k) {
        if(b[k] === v) return;
        r[k] = _.isObject(v)
                ? _.diff(v, b[k])
                : v
            ;
        });
    return r;
}