Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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/7/arduino/2.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
Javascript 如何验证postman测试的数组长度_Javascript_Arraylist_Postman_Newman_Postman Testcase - Fatal编程技术网

Javascript 如何验证postman测试的数组长度

Javascript 如何验证postman测试的数组长度,javascript,arraylist,postman,newman,postman-testcase,Javascript,Arraylist,Postman,Newman,Postman Testcase,我的json响应显示了该对象的4个值(图中显示了3个值)companys[0]。salesReps 如何验证销售员工总数的邮递员测试,并在控制台上打印他们的姓名 x.companies[0].salesReps[0].name x.companies[0].salesReps[1].name x.companies[0].salesReps[2].name 试过这句话未定义 emp = JSON.parse(responseBody(companies[0].salesReps));

我的json响应显示了该对象的4个值(图中显示了3个值)
companys[0]。salesReps
如何验证销售员工总数的邮递员测试,并在控制台上打印他们的姓名

x.companies[0].salesReps[0].name
x.companies[0].salesReps[1].name
x.companies[0].salesReps[2].name

试过这句话未定义

emp = JSON.parse(responseBody(companies[0].salesReps));
    var listcnt = emp.length;
    console.log(listcnt)

缺少任何东西:)谢谢

我认为您最好先解析ResponseBy(在尝试访问响应中的数据之前),因此:


不确定你的反应体的结构,但这应该可以做到。 很高兴适应它,一旦你发布了你的完整回复

const resBody = pm.response.json();

const numberOfSalesEmployees = resBody.companies[0].salesReps.length;

console.log("Number of sales employees:" + numberOfSalesEmployees);

for (var i = 0; i < numberOfSalesEmployees; i++){
    console.log("Name of sales rep " + i + ": " + resBody.companies[0].salesReps[i].name);
}

pm.test("Number of sales employees is 4", function () {
    pm.expect(numberOfSalesEmployees).to.eql(4);
});
const resBody=pm.response.json();
const numberOfSalesEmployees=resBody.companys[0].salesReps.length;
console.log(“销售员工数量:”+numberOfSalesEmployees);
对于(var i=0;i
这只是统计销售代表的数量。问题还要求验证编号并打印销售代表的姓名。谢谢Chris,获得了所有四名销售员工姓名的输出,即“销售代表姓名0:[对象]”。如有要求,请共享完整的回复正文。只有这样,我才能确定如何处理数据并使代码正常工作。感谢Chris,它指向了错误的对象。更新了此行和所有正在工作的内容:)
console.log(“销售代表的姓名”+i+:“+(resBody.companys[0]).salesReps[i].Name)酷。更新了我的答案。感谢您接受它,以防它满足您的要求。接受您的回答:)
const resBody = pm.response.json();

const numberOfSalesEmployees = resBody.companies[0].salesReps.length;

console.log("Number of sales employees:" + numberOfSalesEmployees);

for (var i = 0; i < numberOfSalesEmployees; i++){
    console.log("Name of sales rep " + i + ": " + resBody.companies[0].salesReps[i].name);
}

pm.test("Number of sales employees is 4", function () {
    pm.expect(numberOfSalesEmployees).to.eql(4);
});