从json列表中获取特定值在postman测试中使用javascript
我正在使用postman进行API测试。 我的回复是从json列表中获取特定值在postman测试中使用javascript,javascript,json,postman,Javascript,Json,Postman,我正在使用postman进行API测试。 我的回复是 [ { "firstName": "Jia", "lastName": "Tes", "memberId": 745794, "branchId": 12442, "branchName": "NZ - Clubware Mobile Test Branch 1" }, { "firstName": "Jia",
[
{
"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移动测试分支1”
我现在的代码是
var response = JSON.parse(responseBody);
console.log("BODY:" + response[3].memberId);
但我不喜欢用索引来定位列表中的元素,我该怎么做呢,谢谢大家 var响应=[{
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);
}
}
“姓”:“贾”,
“姓氏”:“Tes”,
“成员ID”:745794,
“布兰奇”:12442,
“branchName”:“新西兰-Clubware移动测试分支1”
},
{
“姓”:“贾”,
“lastName”:“Test2”,
“成员ID”:745746,
“布兰奇”:12442,
“branchName”:“新西兰-Clubware移动测试分支1”
},
{
“姓”:“贾”,
“lastName”:“Test3”,
“成员ID”:745748,
“布兰奇”:12443,
“branchName”:“Clubware移动测试分支2(发布)”
},
{
“姓”:“贾”,
“lastName”:“Test3”,
“成员ID”:745745,
“布兰奇”:12442,
“branchName”:“新西兰-Clubware移动测试分支1”
}
]; var i;
对于(i=0;i如果您只想检查特定值,可以使用Lodash函数获取memberId
值:
var user = _.find(pm.response.json(), {
firstName: "Jia",
lastName: "Test3",
branchName: "NZ - Clubware Mobile Test Branch 1"
})
console.log(user.memberId)
尝试response.find(e=>e.firstName=='Jia'和&e.lastName=='Test3'和&…等)。memberId
-有关数组查找方法@JaromandaX的更多信息,非常感谢,您的建议非常好!