我正在使用邮递员进行API测试。 我的回复信息是
[
{
"firstName": "Jia",
"lastName": "Tes",
"memberId": 745794,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test2",
"memberId": 745746,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745748,
"branchId": 12443,
"branchName": "Clubware Mobile Test Branch 2 (Pub)"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745745,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
}
]
我想得到" memberId"其中" firstName":" Jia"," lastName":" Test3"和" branchName":" NZ - Clubware Mobile Test Branch 1"
我目前的代码就像
var response = JSON.parse(responseBody);
console.log("BODY:" + response[3].memberId);
但我不喜欢使用索引来查找列表中的元素,我该怎么做呢,谢谢你们!
答案 0 :(得分:3)
答案 1 :(得分:2)
var response = [ {
"firstName": "Jia",
"lastName": "Tes",
"memberId": 745794,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test2",
"memberId": 745746,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745748,
"branchId": 12443,
"branchName": "Clubware Mobile Test Branch 2 (Pub)"
},
{
"firstName": "Jia",
"lastName": "Test3",
"memberId": 745745,
"branchId": 12442,
"branchName": "NZ - Clubware Mobile Test Branch 1"
}
]; var i;
for(i =0; i <response.length ; i++)
{
if(response[i].firstName == 'Jia' && response[i].lastName == 'Test3'
&& response[i].branchName == 'NZ - Clubware Mobile Test Branch 1')
{
console.log(response[i].memberId);
}
}