如何循环json数据以与jquery中的另一个json进行比较

时间:2017-06-27 12:33:01

标签: jquery json

我有两个json个对象,

对象1

{
    "ClassificationId": 1,
    "ClassificationSort": 40,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "XYZ",
    "ShortName": "XYZ"
}, {
    "ClassificationId": 2,
    "ClassificationSort": 45,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "ABC",
    "ShortName": "ABC"
}, {
    "ClassificationId": 3,
    "ClassificationSort": 50,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "DEF",
    "ShortName": "DEF"
}

对象2

{
    "ClassificationId": 1,
    "ClassificationSort": 40,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "XYZ_1",
    "ShortName": "XYZ"
}, {
    "ClassificationId": 2,
    "ClassificationSort": 45,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "ABC_1",
    "ShortName": "ABC"
}, {
    "ClassificationId": 3,
    "ClassificationSort": 50,
    "ClassificationType": 146,
    "IsActive": true,
    "Level": 0,
    "LongName": "DEF_1",
    "ShortName": "DEF"
}

我想比较两个objects以找出哪个key已更改。我需要更改keyvalue

请参阅下面的代码段,了解到目前为止我尝试过的内容。然而,它没有按预期工作。



 var result = JSON.parse(json1);
        var result2 = JSON.parse(json2);
        

        $.each(result, function (k, v) {

            var key1 = k;
            var value1 = v;
            $.each(result2, function (k2, v2) {
                if (key1 == k2) {
                    if (value1 != v2)
                        //console.log("key:" + k2 + "value:" + v2);
                        changeData = "[{\"ChangedColumn\":\"" + key1 + "\",\"ChangedValueOld\":\"" + value1 + "\",\"ChangedValueNew\":\"" + v2 + "\",\"RowIdentifierValue\":\"140\"}],";
                    //console.log(changeData);
                    return false;
                }

            });

        });
        




任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您只需循环JSON数组并比较每个键值。有几种方法,但我已经包含了一种简单而有条理的方法来实现这一目标。

这是你的JSON阵列:

var json1 = [{"ClassificationId":1,"ClassificationSort":40,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"XYZ","ShortName":"XYZ"},{"ClassificationId":2,"ClassificationSort":45,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"ABC","ShortName":"ABC"},{"ClassificationId":3,"ClassificationSort":50,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"DEF","ShortName":"DEF"}];

var json2 = [{"ClassificationId":1,"ClassificationSort":40,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"XYZ_1","ShortName":"XYZ"},{"ClassificationId":2,"ClassificationSort":45,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"ABC_1","ShortName":"ABC"},{"ClassificationId":3,"ClassificationSort":50,"ClassificationType":146,"IsActive":true,"Level":0,"LongName":"DEF_1","ShortName":"DEF"}];

以下是javascript代码,它将为您提供在浏览器控制台中打印的差异数组:

var keys = Object.keys(json1[0]);

var changeArray = [];

for(var j=0; j<json1.length; j++){
    var json1Inner = json1[j];
    var json2Inner = json2[j];

    for(var i=0; i<keys.length; i++){
      var key = keys[i], changeTrack = {};
      if(json1Inner[key] !== json2Inner[key]){
         changeTrack['ChangedColumn'] = key;
         changeTrack['ChangedValueOld'] = json1Inner[key];
         changeTrack['ChangedValueNew'] = json2Inner[key];
         changeTrack['RowIdentifierValue'] = '140';

         changeArray.push(changeTrack);
      }
    }
}


console.log(changeArray);

为简单起见,这里是JSFIDDLE