使用 javascript 和 Postman 计算 JSON 数组中的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35987043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting records in JSON array using javascript and Postman
提问by Chrissi
I have a control that returns 2 records:
我有一个返回 2 条记录的控件:
{
"value": [
{
"ID": 5,
"Pupil": 1900031265,
"Offer": false,
},
{
"ID": 8,
"Pupil": 1900035302,
"Offer": false,
"OfferDetail": ""
}
]
}
I need to test via Postman, that I have 2 records returned. I've tried various methods I've found here and elsewhere but with no luck. Using the code below fails to return the expected answer.
我需要通过邮递员进行测试,我返回了 2 条记录。我尝试了在这里和其他地方找到的各种方法,但没有运气。使用下面的代码无法返回预期的答案。
responseJson = JSON.parse(responseBody);
var list = responseBody.length;
tests["Expected number"] = list === undefined || list.length === 2;
At this point I'm not sure if it's the API I'm testing that's at fault or my coding - I've tried looping through the items returned but that's not working for me either. Could someone advise please - I'm new to javascript so am expecting there to be an obvious cause to my problem but I'm failing to see it. Many thanks.
在这一点上,我不确定是我正在测试的 API 有问题还是我的编码有问题 - 我已经尝试遍历返回的项目,但这对我也不起作用。有人可以提供建议 - 我是 javascript 新手,所以我希望我的问题有一个明显的原因,但我没有看到它。非常感谢。
采纳答案by Kaushik
Correct your json. and try this.
更正你的json。试试这个。
var test = JSON.parse('{"value": [{"ID": 5,"Pupil": 1900031265,"Offer": false},{"ID": 8,"Pupil": 1900035302,"Offer": false,"OfferDetail": ""}] }')
test.value.length; // 2
回答by RC_02
In postman, under Tests
section, do the following (screenshot below):
var body = JSON.parse(responseBody);
tests["Count: " + body.value.length] = true;
在邮递员中,在Tests
部分下,执行以下操作(下面的屏幕截图):
var body = JSON.parse(responseBody);
tests["Count: " + body.value.length] = true;
Here is what you should see (note: I replaced responseBody
with JSON to mock up example above):
回答by Forrest
Here's the simplest way I figured it out:
这是我想出的最简单的方法:
pm.expect(Object.keys(pm.response.json()).length).to.eql(18);
No need to customize any of that to your variables. Just copy, paste, and adjust "18" to whatever number you're expecting.
无需为您的变量自定义任何这些。只需复制、粘贴并将“18”调整为您期望的任何数字。
回答by Mustafa Mamun
Your response body is an object you cannot find the length of an object try
您的响应正文是一个对象,您找不到对象的长度 try
var list = responseJson.value.length;
回答by Rakesh133
回答by SweetNoods
More updated version of asserting only 2 objects in an array:
仅断言数组中的 2 个对象的更新版本:
pm.test("Only 2 objects in array", function (){
pm.expect(pm.response.json().length).to.eql(2);
});
回答by J. Castillo
I had a similar problem, what I used to test for a certain number of array members is:
我有一个类似的问题,我用来测试一定数量的数组成员的是:
responseJson = JSON.parse(responseBody);
tests["Response Body = []"] = responseJson.length === valueYouAreCheckingFor;
To check what values you're getting, print it and check the postman console.
要检查您获得的值,请打印它并检查邮递员控制台。
console.log(responseJson.length);
回答by wallance
First of all you should convert response to json and find value path. Value is array. You should call to length function to get how many objects in there and check your expected size
首先,您应该将响应转换为 json 并找到值路径。值是数组。您应该调用 length 函数以获取其中有多少个对象并检查您的预期大小
pm.test("Validate value count", function () {
pm.expect(pm.response.json().value.length).to.eq(2);
});
回答by kartsims
As mentioned in the comments, you should test responseJson.value.length
正如评论中提到的,你应该测试 responseJson.value.length
responseJson = JSON.parse(responseBody);
tests["Expected number"] = typeof responseJson === 'undefined' || responseJson.value.length;
responseJson = JSON.parse(responseBody);
tests["Expected number"] = typeof responseJson === 'undefined' || responseJson.value.length;
回答by Kranti Biswakarma
I was facing similar issue while validating the length of an array inside a JSON. The below snippet should help you resolve it-
我在验证 JSON 中数组的长度时遇到了类似的问题。下面的代码段应该可以帮助您解决它-
responseJson = JSON.parse(responseBody);
var list = responseBody.length;
tests["Expected number"] = responseJson.value.length === list;