我正在使用node.js尝试使用api获取JSON的一些值,我试图遍历并获取JSON中的状态值,我将如何去做。 JSON如下所示
var serverNamesEurope = [
{ name: result.Chars.Servers[0].Server[19].Name, status: result.Chars.Servers[0].Server[19].Usage, return: ""},
{ name: result.Chars.Servers[0].Server[21].Name, status: result.Chars.Servers[0].Server[21].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[9].Name, status: result.Chars.Servers[0].Server[9].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[10].Name, status: result.Chars.Servers[0].Server[10].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[7].Name, status: result.Chars.Servers[0].Server[7].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[14].Name, status: result.Chars.Servers[0].Server[14].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[12].Name, status: result.Chars.Servers[0].Server[12].Usage, return: "" }
]
我想要做的是能够遍历此JSON,然后暂时在控制台的单独一行上返回状态。
答案 0 :(得分:1)
您可以执行以下操作:
var serverNamesEurope = [
{ name: result.Chars.Servers[0].Server[19].Name, status: result.Chars.Servers[0].Server[19].Usage, return: ""},
{ name: result.Chars.Servers[0].Server[21].Name, status: result.Chars.Servers[0].Server[21].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[9].Name, status: result.Chars.Servers[0].Server[9].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[10].Name, status: result.Chars.Servers[0].Server[10].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[7].Name, status: result.Chars.Servers[0].Server[7].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[14].Name, status: result.Chars.Servers[0].Server[14].Usage, return: "" },
{ name: result.Chars.Servers[0].Server[12].Name, status: result.Chars.Servers[0].Server[12].Usage, return: "" }
]
console.log(serverNamesEurope.map(record => record.status).join("\n"))
此处的地图及其工作原理更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
答案 1 :(得分:1)
尝试一下:
serverNamesEurope.forEach(elem => {
console.log(elem.status)
})