【问题标题】:How do I set an Enviromental Variable in Postman, from an array, within an array?如何在 Postman 中从数组中设置环境变量?
【发布时间】:2015-10-26 21:17:05
【问题描述】:

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

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

非常感谢

【问题讨论】:

    标签: javascript arrays testing postman


    【解决方案1】:
    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
    

    【讨论】:

    • 我建议您添加一些额外的行来解释代码的作用以及它如何帮助解决问题吗?
    • 现在添加了 cmets @apokryfos
    • 为什么我的回答得到了 -2 的声望?
    猜你喜欢
    • 2020-08-06
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2016-06-27
    • 2012-08-03
    • 2017-07-31
    相关资源
    最近更新 更多