如何用javascript遍历嵌套的json对象?

kmpatx3s  于 2023-03-16  发布在  Java
关注(0)|答案(4)|浏览(277)

努力从API迭代以下返回json字符串:

{
"data": [
{
  "ActionNeeded": null,
  "OldestStatus": null,
  "ServiceHost": "W501",
  "ServiceName": "Renewals",
  "ServiceStatusDateTime": "2023-03-15T22:47:05.313000",
  "ServiceVersion": "1.0.0",
  "SortOrder": "0",
  "TimeAgo": null
},
{
  "ActionNeeded": null,
  "OldestStatus": null,
  "ServiceHost": "W202",
  "ServiceName": "Monitor",
  "ServiceStatusDateTime": "2023-03-15T22:46:42.560000",
  "ServiceVersion": null,
  "SortOrder": "1",
  "TimeAgo": null
},
{
  "ActionNeeded": null,
  "OldestStatus": null,
  "ServiceHost": "W0070204",
  "ServiceName": "Monitor",
  "ServiceStatusDateTime": "2023-03-15T22:46:39.840000",
  "ServiceVersion": "2.0.2",
  "SortOrder": "1",
  "TimeAgo": null
}
]
]

我尝试过for.. in循环,但我只得到了键/值对的第一个“集合”。
下面是我目前的代码:

var json_obj = JSON.parse(event.data);

// Returned records
const items = json_obj['data'];

for (var item_index in items) {
    if (items.hasOwnProperty(item_index)) {
        console.log(item_index);
    }
}

如何循环遍历每个数据“集”并输出键和值?

brvekthn

brvekthn1#

要循环遍历所提供的嵌套JSON对象,可以使用JavaScript的forEach循环。在本例中,JSON对象有一个名为data的属性,其中包含一个对象数组。可以循环遍历数组中的每个对象,并访问其属性,如下所示:

const jsonObject = {
  "data": [
    //... your JSON data
  ]
};

// Loop through the data array
jsonObject.data.forEach((item) => {
  console.log('ActionNeeded:', item.ActionNeeded);
  console.log('OldestStatus:', item.OldestStatus);
  console.log('ServiceHost:', item.ServiceHost);
  console.log('ServiceName:', item.ServiceName);
  console.log('ServiceStatusDateTime:', item.ServiceStatusDateTime);
  console.log('ServiceVersion:', item.ServiceVersion);
  console.log('SortOrder:', item.SortOrder);
  console.log('TimeAgo:', item.TimeAgo);
  console.log('-------------------');
});

如果您事先不知道属性名称,可以执行以下操作:

for (const item of jsonObject.data) {
  // Loop through the keys and values of the current item
  for (const [key, value] of Object.entries(item)) {
    console.log(`${key}: ${value}`);
  }
  console.log('-------------------');
}
sgtfey8w

sgtfey8w2#

您可以使用Array#forEachfor...of循环访问数组,使用Object.entries获取对象中的所有键值对。

let o={data:[{ActionNeeded:null,OldestStatus:null,ServiceHost:"W501",ServiceName:"Renewals",ServiceStatusDateTime:"2023-03-15T22:47:05.313000",ServiceVersion:"1.0.0",SortOrder:"0",TimeAgo:null},{ActionNeeded:null,OldestStatus:null,ServiceHost:"W202",ServiceName:"Monitor",ServiceStatusDateTime:"2023-03-15T22:46:42.560000",ServiceVersion:null,SortOrder:"1",TimeAgo:null},{ActionNeeded:null,OldestStatus:null,ServiceHost:"W0070204",ServiceName:"Monitor",ServiceStatusDateTime:"2023-03-15T22:46:39.840000",ServiceVersion:"2.0.2",SortOrder:"1",TimeAgo:null}]};
o.data.forEach((x, i) => {
  for (const [k, v] of Object.entries(x)) 
    console.log('index', i, k, '=', v);
});
yh2wf1be

yh2wf1be3#

这里是很好的方法,希望这对你有用!

const eventJsonString = JSON.stringify({
    "data": [
        {
            "ActionNeeded": null,
            "OldestStatus": null,
            "ServiceHost": "W501",
            "ServiceName": "Renewals",
            "ServiceStatusDateTime": "2023-03-15T22:47:05.313000",
            "ServiceVersion": "1.0.0",
            "SortOrder": "0",
            "TimeAgo": null
        },
        {
            "ActionNeeded": null,
            "OldestStatus": null,
            "ServiceHost": "W202",
            "ServiceName": "Monitor",
            "ServiceStatusDateTime": "2023-03-15T22:46:42.560000",
            "ServiceVersion": null,
            "SortOrder": "1",
            "TimeAgo": null
        },
        {
            "ActionNeeded": null,
            "OldestStatus": null,
            "ServiceHost": "W0070204",
            "ServiceName": "Monitor",
            "ServiceStatusDateTime": "2023-03-15T22:46:39.840000",
            "ServiceVersion": "2.0.2",
            "SortOrder": "1",
            "TimeAgo": null
        }
    ]
});

const event = JSON.parse(eventJsonString);
const items = event.data;

items.forEach(item => {
    const { ActionNeeded, OldestStatus, ServiceHost, ServiceName,
        ServiceStatusDateTime, ServiceVersion, SortOrder, TimeAgo } = item;

    console.log(ActionNeeded);
    console.log(OldestStatus);
    console.log(ServiceHost);
    console.log(ServiceName);
    console.log(ServiceStatusDateTime);
    console.log(ServiceVersion);
    console.log(SortOrder);
    console.log(TimeAgo);
    console.log("---------------");
});
uurv41yg

uurv41yg4#

在代码段中,items是一个数组,但是你使用的是对象的数组,你可以把它改成for...of,这样你就不再需要hasOwnProperty了,因为它是对象的键:

var json_obj = JSON.parse(event.data);

// Returned records
const items = json_obj['data'];

for (var item of items) {
    console.log(item);
}

相关问题