繁体   English   中英

如何在 Postman 中从数组中设置环境变量?

[英]How do I set an Enviromental Variable in Postman, from an array, within an array?

我正在努力从数组中的数组中提取数据以设置为变量。 我将如何从地址中列出的 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);

如果这是这个问题的答案,我不太确定如何按照昨天的建议使用控制台日志。

非常感谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM