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

标签 javascript json postman

我正在使用 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"where "firstName": "Jia","lastName": "Test3"and "branchName": "NZ - Clubware Mobile Test Branch 1"

我现在的代码是这样的

var response = JSON.parse(responseBody);
console.log("BODY:" + response[3].memberId);

但是我不喜欢使用索引来定位列表中的元素,我该怎么做,谢谢大家!!

最佳答案

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

var user = _.find(pm.response.json(), { 
    firstName: "Jia", 
    lastName: "Test3", 
    branchName: "NZ - Clubware Mobile Test Branch 1" 
})
console.log(user.memberId)

Postman Response

关于javascript - 从 json 列表中获取特定值在 postman 测试中使用 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50848091/

相关文章:

ruby - 'json"gem 和 'json_pure' gem 有什么区别?

java - 通过 JSON 从 Java 到 Javascript 的 Unicode 字符串

json - 在 Go : required field? 中解码 json

javascript - JavaScript 的日期范围限制

javascript - 这些正则表达式元字符之间有什么区别?

JavaScript 调整大小

javascript - 创建内容为 Latex、文本和图像的 PDF

postman - 如何从 postman 的响应正文中提取值

node.js - 为什么Chrome无法设置Cookie

postman - postman 响应测试中的 OR 运算符