我在这里问,因为我可以看到这个网站,在这方面我有一个JASON格式的输出值,如下所示:
{
"total": 16,
"members": [{
"id": 4,
"name": "Blade11",
"descriptors": {
"os": "Windows 2012 / WS2012 R2"
},
"FCPaths": [{
"wwn": "50060B0000C27208",
"hostSpeed": 0
}, {
"wwn": "50060B0000C2720A",
"hostSpeed": 0
}],
"iSCSIPaths": [],
"persona": 11,
"links": [{
"href": "https://3par:8080/api/v1/hostpersonas?query=\"wsapiAssignedId EQ 11\"",
"rel": "personaInfo"
}],
"initiatorChapEnabled": false,
"targetChapEnabled": false
}, {
"id": 6,
"name": "Blade4",
"descriptors": {
"os": "VMware (ESXi)"
},
"FCPaths": [{
"wwn": "50060B0000C27216",
"hostSpeed": 0
}, {
"wwn": "50060B0000C27214",
"hostSpeed": 0
}],
"iSCSIPaths": [],
"persona": 8,
"links": [{
"href": "https://3par:8080/api/v1/hostpersonas?query=\"wsapiAssignedId EQ 8\"",
"rel": "personaInfo"
}],
"initiatorChapEnabled": false,
"targetChapEnabled": false
}我想要的是,解析此输出,以便仅在字符串列表或字符串数组中检索名称对象的输出参数,例如Names = Blade11,Blade4,...
如果您可以在Json输出中看到,我们将所有名称都放在"members“下,那么每个名称都是另一个值数组,我只想检索名称
谢谢
发布于 2017-03-02 01:51:59
因为已经有了JSON格式,所以可以在JSON对象的成员键上使用数组方法来迭代每个数组项。
var names = [];
object_name.members.forEach((arrItem) => {
names.push(arrItem.name);
}或
namesArray = object_name.members.map((arrItem) => {
return arrItem.name;
}发布于 2017-03-02 01:53:38
您可以使用Array#map来迭代数组的所有元素,并且只返回name属性。
如果您有一个JSON字符串,则需要提前对其进行解析以获取对象,例如
object = JSON.parse(jsonString);
var jsonString = '{"total":16,"members":[{"id":4,"name":"Blade11","descriptors":{"os":"Windows 2012 / WS2012 R2"},"FCPaths":[{"wwn":"50060B0000C27208","hostSpeed":0},{"wwn":"50060B0000C2720A","hostSpeed":0}],"iSCSIPaths":[],"persona":11,"links":[{"href":"https://3par:8080/api/v1/hostpersonas?query=\\"wsapiAssignedId EQ 11\\"","rel":"personaInfo"}],"initiatorChapEnabled":false,"targetChapEnabled":false},{"id":6,"name":"Blade4","descriptors":{"os":"VMware (ESXi)"},"FCPaths":[{"wwn":"50060B0000C27216","hostSpeed":0},{"wwn":"50060B0000C27214","hostSpeed":0}],"iSCSIPaths":[],"persona":8,"links":[{"href":"https://3par:8080/api/v1/hostpersonas?query=\\"wsapiAssignedId EQ 8\\"","rel":"personaInfo"}],"initiatorChapEnabled":false,"targetChapEnabled":false}]}',
object = JSON.parse(jsonString),
array = object.members.map(function (a) { return a.name; });
console.log(array);
发布于 2017-03-02 01:54:48
如果此JSON是字符串,则必须先对其进行解析
var json = JSON.parse('here is your JSON string');然后,您就可以像使用对象一样访问它的属性
var names = json.members.map(function(member) {
return member.name;
});https://stackoverflow.com/questions/42538582
复制相似问题