JSON对象转换中的嵌套数组

时间:2017-02-01 06:59:02

标签: javascript arrays

我正在努力转换我拥有的嵌套JSON数组。

{
  "Id": "1234",
  "Company": {
    "element": [{
      "Name": "htc",
      "Contacts": {
        "element": [{
          "name": "john",
          "phone": "1234"
        }, {
          "name": "peter",
          "phone": "5678"
        }]
      },
      "Address": {
        "element": {
          "country": "us",
          "state": "cali"
        }
      }
    }, {
      "Name": "samsung",
      "Contacts": {
        "element": [{
          "name": "luke",
          "phone": "0011"
        }, {
          "name": "james",
          "phone": "2233"
        }]
      },
      "Address": {
        "element": {
          "country": "us",
          "state": "texas"
        }
      }
    }]
  }
}

正如您将注意到的,数组“公司”,“联系人”和“地址”中存在此“元素”。但是我需要提供的输出不应该包含“元素”,例如此代码:

{
  "Id": "1234",
  "Company": [{
    "Name": "htc",
    "Contacts": [{
      "name": "john",
      "phone": "1234"
    }, {
      "name": "peter",
      "phone": "5678"
    }],
    "Address": [{
      "country": "us",
      "state": "cali"
    }]
  }, {
    "Name": "samsung",
    "Contacts": [{
      "name": "luke",
      "phone": "0011"
    }, {
      "name": "james",
      "phone": "2233"
    }],
    "Address": [{
      "country": "us",
      "state": "texas"
    }]
  }]
}

我不清楚在JavaScript中如何做。任何想法/提示都很感激。 谢谢

4 个答案:

答案 0 :(得分:0)

使用Array.prototype.forEach()函数的解决方案:



var companyData = { "Id": "1234", "Company": { "element": [{ "Name": "htc", "Contacts": { "element": [{ "name": "john", "phone": "1234" }, { "name": "peter", "phone": "5678" }] }, "Address": { "element": { "country": "us", "state": "cali" } } }, { "Name": "samsung", "Contacts": { "element": [{ "name": "luke", "phone": "0011" }, { "name": "james", "phone": "2233" }] }, "Address": { "element": { "country": "us", "state": "texas" } } }] }
};

companyData.Company = companyData.Company.element;
var omitElement = function(o){
    if (!o['element']) return o;
    return (Array.isArray(o.element))? o.element : [o.element];
}
companyData.Company.forEach(function (o) {
    o.Contacts = omitElement(o.Contacts);
    o.Address = omitElement(o.Address);
});

console.log(companyData);




答案 1 :(得分:0)

您可以尝试这样的事情:



var data={Id:"1234",Company:{element:[{Name:"htc",Contacts:{element:[{name:"john",phone:"1234"},{name:"peter",phone:"5678"}]},Address:{element:{country:"us",state:"cali"}}},{Name:"samsung",Contacts:{element:[{name:"luke",phone:"0011"},{name:"james",phone:"2233"}]},Address:{element:{country:"us",state:"texas"}}}]}};

var keysToClean = ["Address", "Contacts"]

// Copy object instead of reference
var result = Object.assign({}, data);

result.Company = result.Company.element;
result.Company.forEach(x => {
  keysToClean.forEach(k => {
    x[k] = Array.isArray(x[k]) ? x[k].element : [x[k].element]
  })
})

console.log(result);




注意:我使用了Object.createArrow functions。旧浏览器不支持它们。您可以参考以下链接以替代深层复制对象:

答案 2 :(得分:0)

请参阅此Plunker这应该有所帮助..它将产生您需要的所需结果,但请注意这只是一种方法,并且仅用于信息目的。它不是生产等级......

function ParseData(data)
{
  var newObject={Id:0, Company:[]};

  newObject["Id"]=data["Id"];
  newObject["Company"]=CreateCompanyObject(data["Company"]["element"]);

  console.log(JSON.stringify(newObject));
}


function CreateCompanyObject(data)
{
  var companies=[];
 for(var i=0;i<data.length;i++)
 {
   companies.push({
                    name:data[i].Name, 
                    contacts:CreateContactObject(data[i].Contacts.element), 
                    Address:CreateAddressObject(data[i].Address.element)});

 };

  return companies;
}

function CreateContactObject(data){
  var contacts=[];
  for(var i=0;i<data.length;i++)
    contacts.push(data[i]);

  return contacts;
}

function CreateAddressObject(data){
  var address=[];
    if(typeof(data)=="array"){
    for(var i=0;i<data.length;i++)
    address.push(data[i]);
    }
    else
    address.push(data);

  return address;
}

答案 3 :(得分:0)

您可以检查element并将内容向前移动到其父级。

&#13;
&#13;
function deleteElement(object){
    Object.keys(object).forEach(function (k) {
        if (object[k] && typeof object[k] === 'object') {
            if ('element' in object[k]) {
                object[k] = Array.isArray(object[k].element) ?
                    object[k].element :
                    [object[k].element];
            }
            deleteElement(object[k]);
        }
    });
}


var data = { Id: "1234", Company: { element: [{ Name: "htc", Contacts: { element: [{ name: "john", phone: "1234" }, { name: "peter", phone: "5678" }] }, Address: { element: { country: "us", state: "cali" } } }, { Name: "samsung", Contacts: { element: [{ name: "luke", phone: "0011" }, { name: "james", phone: "2233" }] }, Address: { element: { country: "us", state: "texas" } } }] } };

deleteElement(data);
console.log(data);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;