从json列表中获取特定值,在postman测试中使用javascript

时间:2018-06-14 00:36:14

标签: javascript json postman

我正在使用邮递员进行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);

但我不喜欢使用索引来查找列表中的元素,我该怎么做呢,谢谢你们!

2 个答案:

答案 0 :(得分:3)

如果您只是想检查特定值,可以使用_.find() Lodash函数获取{{1}}值:

{{1}}

Postman Response

答案 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);
  }
}