Postman 如何从列表中迭代获取JSON响应值

sdnqo3pr  于 2023-03-18  发布在  Postman
关注(0)|答案(1)|浏览(242)

我有以下JSON响应,在测试中,我创建了一个要记录/捕获的键列表,但输出返回错误:类型错误:responseData.get不是函数,因为在解释“responseData.i”时作为字符串读取。

JSON response:
{
"Id": 1287,
"LastName": "Trump",
"FirstName": "Donald",
"MiddleName": "Von",
"City": "New York City",
"Phone": "66 77 88",
"State": "New York",
"Fax": "111-222-333",
"ReferenceId": "12345",
"Active": false,
"CurrentWorkingSchemeId": null
}

var responseData = pm.response.json()
var list_data = \["Id", "Fax", "Active"\];
for (i of list_data) {
console.log(responseData.Id, responseData.Fax, responseData.Active);
console.log(i, responseData.i);
console.log(i, responseData.get(i));
}

我需要做什么:响应数据.i =响应数据.Id不是响应数据.i =响应数据.“Id”
谢谢

w8f9ii69

w8f9ii691#

正确的语法为console.log(i, responseData[i]);

相关问题