简体   繁体   English

发现 JSON 在 JSON Object 中发生变化

[英]Find JSON is Changed in JSON Object

I'm working request data from a URL for every seconds我每秒钟都在处理来自 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);

});

} }

just like this and every seconds i want to check that the Array JSON that I recieved which json is different from the previous one then I can send the JSON that changed through socket.io and update in the table of client-side just like this and every seconds i want to check that the Array JSON that I recieved which json is different from the previous one then I can send the JSON that changed through socket.io and update in the table of client-side

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 Example, it's array that have many json object in there the example is just one json JSON 示例,它的数组有许多 json object 示例中只有一个 Z466DEEC76ECDF324FCA6DZ857541

[{"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}}]}]

The flow is like this Run app.js -> Server side: request data -> Server side: send whole data to client -> Client: fetch all data into table -> next 1 seconds -> Server side: request data - > Server side: compare the old data and new data and find the json that change - > Server side: send the json that different from the previous one to client -> Client: Update the new json to table流程是这样的运行 app.js -> 服务器端:请求数据 -> 服务器端:将整个数据发送到客户端 -> 客户端:将所有数据提取到表中 -> 下 1 秒 -> 服务器端:请求数据 -> 服务器侧:比较旧数据和新数据,找到变化的json -> 服务器端:将与上一个不同的json发送给客户端 -> 客户端:更新新的json到表

I don't think I got you right.. If you just want to check if there is a difference , you could simply compare stings?..我不认为我说对了..如果您只是想检查是否有区别,您可以简单地比较刺痛吗?...

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

That would tell you if there is a change at all.这会告诉你是否有变化。 To find the difference you can do strings manipulations or use underscore Lib (_) to help you out.找到差异,您可以进行字符串操作或使用下划线 Lib (_) 来帮助您。

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

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

Results in the parts of o1 that correspond but with different values in o2:结果在 o1 中对应但在 o2 中具有不同值的部分:

{a: 1, b: 2}

It'd be different for a deep diff:深度差异会有所不同:

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

if want to find what changed inside json, you may consider using json-diff package如果想找出 json 内部发生了什么变化,可以考虑使用json-diff package

https://www.npmjs.com/package/json-diff https://www.npmjs.com/package/json-diff

From readme:来自自述文件:

let jsonDiff = require('json-diff')

console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' }));
// Output:
//  {
// -  foo: "bar"
// +  foo: "baz"
//  }

// As above, but without console colors
console.log(jsonDiff.diffString({ foo: 'bar' }, { foo: 'baz' }, {color:false}));

// Raw output:
console.log(jsonDiff.diff({ foo: 'bar', b:3}, { foo: 'baz', b:3}));
// Output:
// { foo: { __old: 'bar', __new: 'baz' } }

// Passing in the "full" option:
console.log(jsonDiff.diff({ foo: 'bar', b:3}, { foo: 'baz', b:3}, {full:true}));
// Output:
// { foo: { __old: 'bar', __new: 'baz' }, b: 3 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM