将JSON对象与JSON子对象合并

时间:2015-12-10 17:10:36

标签: javascript json object merge

我觉得我错过了一些非常明显的东西......我无法让JSON对象合并并保存它们的子对象。我需要合并四个JSON对象。

Customer: {"datecreated":"12/10/2015 9:18 am","id":"5566447","entityid":"652967804","entitystatus":"13","companyname":"ABCTools Inc parent","altname":"ABCTools Inc parent","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"}

Contact: {"contact":{"datecreated":"12/10/2015 9:18 am","id":"5566544","entityid":"JohnDoe Customer","firstname":"JohnDoe","lastname":"Contact","email":"jdoe@email.com","phone":"(512) 123-4567","company":"5566447","subsidiary":"1"}}

SubCustomer: {"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"}

subContact: {"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

我可以将它们与问题合并。但是,他们不保留自己的结构。下面是我最初用来合并它们的函数:

function concatObjects(cust,contact){
  for(var key in contact){
    cust[key]=contact[key];
  }
  return cust;
}

这对客户和联系人非常有用:

var mergedCust=concatObjects(customer,contact);

mergedCust: {"datecreated":"12/10/2015 9:18 am","id":"5566447","entityid":"652967804","entitystatus":"13","companyname":"ABCTools Inc parent","altname":"ABCTools Inc parent","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1","contact":{"datecreated":"12/10/2015 9:18 am","id":"5566544","entityid":"JohnDoe Customer","firstname":"JohnDoe","lastname":"Contact","email":"jdoe@email.com","phone":"(512) 123-4567","company":"5566447","subsidiary":"1"}}

但是,当我在Subcustomer和子联系上尝试时,他们只是互相倾诉,就像这样:

mergedSubCust: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

我可以看到这是因为它们是子对象。所以我尝试了这个:

function concatObjects(cust,contact){
    for(var key in contact){
      if(typeof contact[key]=='object'){
        cust[key]={};
        for(var subKey in contact){
          cust[key]=contact[subKey];
        }
      }else{
        cust[key]=contact[key];
      }
    }
    return cust;
  }

但是,它仍然会返回相同的内容:

mergedSub: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

以下是所有合并后的外观:

{
  "datecreated": "12\/10\/2015 9:18 am",
  "id": "5566447",
  "entityid": "652967804",
  "entitystatus": "13",
  "companyname": "ABCTools Inc parent",
  "altname": "ABCTools Inc parent",
  "email": "jdoe@email.com",
  "phone": "(512) 123-4567",
  "subsidiary": "1",
  "contact": {
    "datecreated": "12\/10\/2015 9:18 am",
    "id": "5566544",
    "entityid": "JohnDoe Customer",
    "firstname": "JohnDoe",
    "lastname": "Contact",
    "email": "jdoe@email.com",
    "phone": "(512) 123-4567",
    "company": "5566447",
    "subsidiary": "1"
  },
  "subcustomer": {
    "datecreated": "12\/10\/2015 9:18 am",
    "id": "5566448",
    "entityid": "652967805",
    "entitystatus": "13",
    "companyname": "ABCTools Inc sub",
    "altname": "ABCTools Inc sub",
    "email": "jdoe@email.com",
    "phone": "(512) 123-4567",
    "subsidiary": "1",
    "subcontact": {
      "datecreated": "12\/10\/2015 7:14 am",
      "id": "5566142",
      "entityid": "JohnDoe SubCustomer",
      "firstname": "Johndoe",
      "lastname": "Subcustomer",
      "email": "jdoe@email.com",
      "phone": "(512) 123-4567",
      "company": null,
      "subsidiary": "1"
    }
  }
}

我相信我所缺少的是如何将整个第二个对象放在第一个对象中,无论数据类型是什么。

我已经搜索了很多内容,但没有找到任何针对此问题的内容......感谢您对此有任何了解。

1 个答案:

答案 0 :(得分:1)

假设他们现在是一个json的一部分

var oneObj = {
   customer: {},
   contact: {},
   subcustomer: {},
   subcontact: {}
};

创建另一个对象

var finalObj = {};
finalObj.customer = oneObj.customer;
finalObj.customer.contact = oneObj.contact;
finalObj.customer.subcustomer = oneObj.subcustomer;
finalObj.customer.subcustomer.subcontact = oneObj.subcontact;