【问题标题】:How to compare two object JSON using AngularJs?如何使用 AngularJs 比较两个对象 JSON?
【发布时间】:2016-09-05 10:49:59
【问题描述】:

我有两个具有层次结构的对象 JSON。我需要在 JSON 中比较行集中的每个对象。值不相等的地方 然后我必须在相应的对象上添加一个标志。

请查看我的 JSON 并为此提供解决方案。如果不是角度至少我必须在 Javascript 中实现。

提前谢谢...

JSON1

{"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}}

JSON2

{"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Vijay","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"SilkBoard","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}}

【问题讨论】:

  • 给对应的对象加一个标志是什么意思?
  • 你可以用angular.equals(obj1, obj2);比较两个对象。
  • @JrBenito - 我需要得到不匹配的确切对象值。
  • @tibsar - 我想要完全不匹配的对象值。在此示例中 JSON 对象 Name "Customer29Jan16" , Name 属性不同于另一个 JSON 。我需要找出索引以在那边添加标志。

标签: javascript angularjs


【解决方案1】:

我已经为您制作了一个起始示例。你可以玩here

JavaScript

var json1 = {"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}};

var json2 = {"entityinfo":{"entity":"Customer29Jan16","tenantid":"292FEC76-5F1C-486F-85A5-09D88096F098","timestamp":"2015-12-15T10:16:06.322Z"},"collections":{"Customer29Jan16":{"rowset":[{"CuId":"123","Name":"Vijay","Quantity":"60","Rate":"60","Amount":"3600"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]},"Customer29Jan16Obj":{"rowset":[{"CuObjId":"456","FullAddress":"SilkBoard","ObjAddr":"Bangalore","ObjName":"Testing"}],"meta":{"parentreference":"***","pkname":"***","fkname":"***"},"rowfilter":[]}}};

function compareJSON(json1, json2) {
  var objectsDiffering = [];
  compareJSONRecursive(json1, json2, objectsDiffering);
  return objectsDiffering;
}

function compareJSONRecursive(json1, json2, objectsDiffering) {
  for(prop in json1) {
    if(json2.hasOwnProperty(prop)) {
        switch(typeof(json1[prop])) {
        case "object":
            compareJSONRecursive(json1[prop], json2[prop], objectsDiffering);
          break;
        default:
          if(json1[prop] !== json2[prop]) {
            objectsDiffering.push(json1);
          }
          break;
      }
    } 
    else {
      objectsDiffering.push(json1);
      break;
    }
  }
}

var differing = compareJSON(json1, json2);
console.log(JSON.stringify(differing));
//Logs: [{"CuId":"123","Name":"Ranjini","Quantity":"60","Rate":"60","Amount":"3600"},{"CuObjId":"456","FullAddress":"Electronic City","ObjAddr":"Bangalore","ObjName":"Testing"}]

【讨论】:

  • 感谢您的代码。现在我有一个小问题。当现有对象属性不匹配时,我需要添加“operationContext:IsUpdate”。同时整个对象在 JSON1 中不存在并且在 JSON2 中存在,在这种情况下我需要添加“operationContext:IsAdded”。我希望你明白我的意思。
  • @bagya 在objectsDiffering.push(json1); 之前添加这个。你想在对象上设置一个属性吗?
  • 是的..请看看小提琴。 jsfiddle.net/bagya1985/y9efgzh6/2。这个小提琴用于匹配对象更改。假设不匹配的对象。我需要在哪里包括“”operationContext:IsAdded”。
  • @bagya 您在json2 上设置属性,json1 是要添加的对象。也许你想以某种方式切换它?
  • 这里我在 JSON2 中新添加了对象属性。我可以将它设为 JSON1 或 JSON2,这没有问题。但是我如何才能仅为新添加的对象添加“operationContext:IsAdded”。请查看我的小提琴链接jsfiddle.net/bagya1985/y9efgzh6/2 和 JSON2
【解决方案2】:

使用 angular.equals(o1,o2) 。它进行深度比较并且不依赖于键的顺序

angular.equals(JSON1, JSON2); // return boolean (true or false) based on the comparison 

参考:Angularjs Docs

【讨论】:

  • 它将给出真或假。我想要完全不匹配的对象值。在此示例中 JSON 对象 Name "Customer29Jan16" , Name 属性不同于另一个 JSON 。我需要找出索引以在那边添加标志。我希望你能得到我的要求。
  • @bagya 看看这个:stackoverflow.com/questions/4465244/compare-2-json-objects你可以修改它以满足你的需要
猜你喜欢
  • 1970-01-01
  • 2019-03-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多