Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过json进行迭代,并对其他请求调用多个API_Java_Arrays_Api_Postman - Fatal编程技术网

Java 通过json进行迭代,并对其他请求调用多个API

Java 通过json进行迭代,并对其他请求调用多个API,java,arrays,api,postman,Java,Arrays,Api,Postman,我正在使用Postman来迭代一个包含大约40对项目的json。然后,我需要获取创建的数组,并为数组中的每个元素运行API调用,以返回一组结果。使用这里的代码,我只能提取数组中的最后一个元素。我试图将postman.setNextRequest放入for循环,但后来我发现无论它在哪里,它总是最后执行 tests["Status code is 200 (that's good!)"] = (responseCode.code === 200); if (responseCode.code ==

我正在使用Postman来迭代一个包含大约40对项目的json。然后,我需要获取创建的数组,并为数组中的每个元素运行API调用,以返回一组结果。使用这里的代码,我只能提取数组中的最后一个元素。我试图将postman.setNextRequest放入for循环,但后来我发现无论它在哪里,它总是最后执行

tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);

if (responseCode.code === 200) {

var jsonData = pm.response.json();
var json = [];

postman.setEnvironmentVariable("json", jsonData)
postman.setNextRequest('GetAdmins');

for (var key in jsonData ) {
    if (jsonData.hasOwnProperty(key)) {
        postman.setEnvironmentVariable("organizationId", jsonData[key].id)
        postman.setEnvironmentVariable("orgname", jsonData[key].name)
        tests[jsonData[key].name + " " + jsonData[key].id] = !!jsonData[key].name;
        }
    }
}

else {
    postman.setNextRequest(null);
}
GetAdmins是另一个在调用中使用{{organizationId}的GET

我想我要找的是;对json中的每个元素运行另一个API调用的最佳方式是什么

提前谢谢

编辑:添加JSON输出

[
    {
        "id": XXXXXX,
        "name": "Name1"
    },
    {
        "id": XXXXXX,
        "name": "Name2"
    },
    {
        "id": XXXXXX,
        "name": "Name3"
    }
]

这可能有助于获取数据——虽然我还没有尝试过,但第一次可能不起作用

var jsonData = pm.response.json()

data = _.map(jsonData, item => {
         organizationId: item.id
         orgName: item.name
     })
pm.environment.set('organizationData', JSON.stringify(data))
然后,您将所有组织数据放在一个变量中,您可以在下一个“Get Admins”请求中使用这些数据来迭代Id

您需要在下一个请求的
Pre-request script
中包含一些代码,才能访问每个id,以便在请求中进行迭代。您需要像这样解析变量:

var orgID = pm.environment.get(JSON.parse("organizationData"))
然后,
orgID[0]。organizationId
将是列表中的第一个


这不是解决您问题的完整解决方案,但可能有助于您获取数据

我通过以下两个指南解决了这个问题:

  • 我还必须为java实现bigint修复程序,但在Postman中,这非常烦人。。。可以在这里找到:

  • 谷歌加上大量的尝试和错误让我开始运行

    谢谢大家的帮助

    这是我最后的代码:

    GetOrgs

    tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);
    
    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    var id_list = [];
    
    jsonData.forEach(function(list) {
        var testTitle = "Org: " + list.name + " has id: " + JSON.stringify(list.id);
        id_list.push(list.id);
        tests[testTitle] = !!list.id;
    });
    
    postman.setEnvironmentVariable("organizationId",JSON.stringify(id_list.shift()));
    postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
    postman.setNextRequest("GetAdmins");
    
    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    
    jsonData.forEach(function(admin) {
        var testTitle = "Admin: " + admin.name + " has " + admin.orgAccess;
        tests[testTitle] = !!admin.name;
    });
    
    var id_list = JSON.parse(environment.id_list);
    if (id_list.length > 0) {
        postman.setEnvironmentVariable("organizationId", JSON.stringify(id_list.shift());
        postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
        postman.setNextRequest("GetAdmins");
    }
    
    else {
        postman.clearEnvrionmentVariable("organizationId");
        postman.clearEnvironmentVariable("id_list");
    }
    
    GetAdmins

    tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);
    
    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    var id_list = [];
    
    jsonData.forEach(function(list) {
        var testTitle = "Org: " + list.name + " has id: " + JSON.stringify(list.id);
        id_list.push(list.id);
        tests[testTitle] = !!list.id;
    });
    
    postman.setEnvironmentVariable("organizationId",JSON.stringify(id_list.shift()));
    postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
    postman.setNextRequest("GetAdmins");
    
    eval(postman.getGlobalVariable("bigint_fix"));
    
    var jsonData = JSON.parse(responseBody);
    
    jsonData.forEach(function(admin) {
        var testTitle = "Admin: " + admin.name + " has " + admin.orgAccess;
        tests[testTitle] = !!admin.name;
    });
    
    var id_list = JSON.parse(environment.id_list);
    if (id_list.length > 0) {
        postman.setEnvironmentVariable("organizationId", JSON.stringify(id_list.shift());
        postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
        postman.setNextRequest("GetAdmins");
    }
    
    else {
        postman.clearEnvrionmentVariable("organizationId");
        postman.clearEnvironmentVariable("id_list");
    }
    

    var json=[]
    在这里做什么。看起来你并没有在推动什么。看起来它每次运行时都会覆盖上一个env变量集。此外,您还可以在Postman中使用Lodash将这些值映射到键。您还可以将
    pm.response.json()
    的输出添加到问题中吗?请。@DannyDainton-var json被用作在API调用之间传递的环境变量。我将查看Lodash。我可以看到设置了
    json
    Postman变量,但上面一行中有一个
    var json=[]
    ,与此无关。看起来它试图在数组中收集一些东西,但它什么也不做。bigInt修复程序在您的上下文中做了什么?所有的解决方案都使用了较旧的postman语法,因此我建议检查pm.*api,因为这种较旧的语法最终将被删除。@DannyDainton-很抱歉,呃,回复太晚了。使用Bigint是因为返回的值非常非常大。没有它,Java/Postman就无法解释它看到的值。