Javascript JSON 比较

编程入门 行业动态 更新时间:2024-10-23 19:18:01
本文介绍了Javascript JSON 比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试构建一个从服务器获取数据并将其显示给用户的 web 应用程序.脚本每 10 秒从服务器获取一次数据,如果数据发生变化,它会提醒用户.这是我现在使用的代码,但它每 10 秒提醒一次数据是否已更改.

I am trying to build a webapp that gets data from server and shows it to user. Script gets data from server every 10 seconds and if data has changed it alerts user. This is the code I'm using now, but it alerts every 10 second whether the data has changed or not.

那么我需要如何更改我的 scipt 以使其比较旧 JSON 和新 JSON 并查看它们是否不同,以及在更新显示给用户的数据之前它们是否显示警报?

So how do I need to alter my scipt to make it compare the old JSON and the new JSON and see if they are different, and if they are show alert before updating data shown to user?

$('#ListPage').bind('pageinit', function(event) { getList1(); }); setInterval ( "getList1()", 10000 ); var old = ""; function getEmployeeList1() { $.getJSON(serviceURL + 'getemployees.php?' + formArray, function(data) { if(data != old){ // data from the server is not same as old $('#nollalista li').remove(); keikka = data.key; $.each(keikka, function(index, lista) { $('#nollalista').append('<li><a href="employeedetails.html?id=' + lista.IND + '">' + '<h4>' + lista.OSO + '</h4>' + '<p>' + lista.AIKA + '</p>' +'</a></li>'); }); $('#nollalista').listview('refresh'); if(old != "") alert("New data!"); old = data; } }); }

推荐答案

一个非常简单(但有点蹩脚)的解决方案是比较字符串表示:

A very easy (but kind of lame) solution is comparing the string representations:

if(JSON.stringify(a) != JSON.stringify(b)) { ... }

更多推荐

Javascript JSON 比较

本文发布于:2023-10-24 14:45:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!