JSON获取嵌套信息

时间:2018-08-24 16:51:15

标签: json

我需要从一些JSON代码获取信息,所以我拥有的数据是:

{
        "lastUpdate": "2018-07-24T18:23:44.7530145",
        "ownerTeam": "Administradores",
        "createdDate": "2015-03-23T03:00:00",
        "baseStatus": "New",
        "status": "Novo",
        "urgency": "0.Baixa",
        "subject": "SPED PIS E COFINS",
        "id": 6408,
        "owner": {
            "id": "99",
            "personType": 1,
        }
 }

我想获取嵌套之外的嵌套信息,例如“ personType”。像这样:

{
        "lastUpdate": "2018-07-24T18:23:44.7530145",
        "ownerTeam": "Administradores",
        "createdDate": "2015-03-23T03:00:00",
        "baseStatus": "New",
        "status": "Novo",
        "urgency": "0.Baixa",
        "subject": "SPED PIS E COFINS",
        "id": 6408,
        "personType": 1,

}

看到我删除了“所有者” “ id”:“ 99” 标签,但“ personType”:1 仍然存在。可以这样做吗?

1 个答案:

答案 0 :(得分:0)

如果您使用JavaScript进行编辑,则可以轻松地做到这一点。

//Original JSON object
var j = {
  lastUpdate: '2018-07-24T18:23:44.7530145',
  ownerTeam: 'Administradores',
  createdDate: '2015-03-23T03:00:00',
  baseStatus: 'New',
  status: 'Novo',
  urgency: '0.Baixa',
  subject: 'SPED PIS E COFINS',
  id: 6408,
  owner: {
  	id: '99',
    personType: 1,
  }
};
console.log(j);

//Set the personType in the root object
j.personType = j.owner.personType;
console.log(j);
//Remove the owner element from the object
delete j.owner;
console.log(j);