我正在努力从数组中的数组中提取数据以设置为变量。如何从地址
中列出的ID设置变量{
"user profile":{
"email_address" : "test@test.com",
"addresses" : [
{
"id": 1
},
{
"id": 2
},
]
对于单个阵列,我使用
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("AddressID",data.user_address[data.addresses.length-1].id);
我不太清楚如何使用昨天建议的控制台日志,如果这是这个问题的答案。
非常感谢
答案 0 :(得分:2)
var data = JSON.parse(responseBody); // this will store api response into variable named as data
var len=data.addresses.length; // this is evaluating length of array named as addresses in api response and saving length in variable named as len.
var envvar_akeyvalue=[]; // Here a variable is declared named as envvar_akeyvalue and variable type is defined as array using =[], to begin with it's an empty array
var j=0; //Another variable declared named as j
for (var i=0; i<len; i++) // Here a loop is executed starting from array position 0 until value of len obtained in line 2 of code
{
envvar_akeyvalue[j]=data.addresses[i].id; j=j+1; //this is capturing value of id from addresses array and saving it an array named as envvar_akeyvalue
}
postman. setEnvironmentVariable("id", envvar_akeyvalue); // this is telling postman to set environment variable with name id and obtain it's value(s) from variable envvar_akeyvalue