var AdchinaJson = {};
function aaa(){
var a ={
a:"111",
b:"222",
c:[{
a:"333",
b:"444",
d:"555"
}]
}
var b={
a:"111",
b:"223",
c:[{
a:"333",
b:"444",
c:"444"
}]
}
AdchinaJson.Getdifferent(a,b);
console.log(AdchinaJson);
console.log(AdchinaJson.Json2Str(a));
console.log(AdchinaJson.Json2Str(b));
}
AdchinaJson.Getdifferent = function (json1, json2) {
for (var key in json1) {//循环遍历其中一个json对象
if (typeof (json1[key]) != "object") {
if (json2[key] != null) {
if (json1[key] == json2[key]) {
delete json1[key];
delete json2[key];
}
}
}
else {
if (json1[key].length >= 0) {
for (i = 0; i < json1[key].length; i++) {
this.Getdifferent(json1[key][i], json2[key][i]);
}
}
else {
this.Getdifferent(json1[key], json2[key]);
}
}
}
}
AdchinaJson.Json2Str = function (o) {
var arr = [];
var fmt = function (s) {
if (typeof s == 'object' && s != null) return AdchinaJson.Json2Str(s);
return /^(string|number)$/.test(typeof s) ? "'" + s + "'" : s;
}
for (var i in o) arr.push("'" + i + "':" + fmt(o[i]));
return '{' + arr.join(',') + '}';
}
对比json对象
最新推荐文章于 2024-07-22 19:51:18 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)