Javascript JSON比较

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

我试图构建一个webapp从服务器获取数据并显示给用户。脚本每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:44:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524210.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Javascript   JSON

发布评论

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

>www.elefans.com

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