[英]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);
});
}
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 示例,它的數組有許多 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}}]}]
流程是這樣的運行 app.js -> 服務器端:請求數據 -> 服務器端:將整個數據發送到客戶端 -> 客戶端:將所有數據提取到表中 -> 下 1 秒 -> 服務器端:請求數據 -> 服務器側:比較舊數據和新數據,找到變化的json -> 服務器端:將與上一個不同的json發送給客戶端 -> 客戶端:更新新的json到表
我不認為我說對了..如果您只是想檢查是否有區別,您可以簡單地比較刺痛嗎?...
JSON.stringify(a) === JSON.stringify(b)
這會告訴你是否有變化。 要找到差異,您可以進行字符串操作或使用下划線 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; })
結果在 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;
}
如果想找出 json 內部發生了什么變化,可以考慮使用json-diff
package
https://www.npmjs.com/package/json-diff
來自自述文件:
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.