【发布时间】:2020-01-04 00:48:33
【问题描述】:
我试图将断言放在邮递员数组中。我试图在数组中检索数组,但它有错误。
我们将不胜感激任何建议。
{
"meta": {
"code": 200
},
"data": [
[
{
"_index": "test",
"_type": "test",
"_id": "sdgsdfg",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
},
{
"_index": "test",
"_type": "test",
"_id": "test",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
},
]
]
}
到目前为止,我已经尝试过这个,但它给出了错误。
var jsonData = JSON.parse(responseBody);
pm.test("Checking Success Response on Service", function () {
pm.response.to.have.status(201);
});
for(var i=0; i< jsonData.data.length ;i++){
for(var j=0; j< jsonData.data[i].length ;j++){
console.log(jsonData.data[i].j._index);
}
}
评估测试脚本时出错:TypeError: Cannot read property '_index' of undefined
请告诉我可以做什么。
【问题讨论】:
-
嵌套数组所以
jsonData.data[i][j]._index -
工作正常。但我不明白这里的逻辑,里面有第二个数组。它也有单独的对象。
-
jsonData.data[i].j._index当你说j时,它的字面意思是j。您需要说[j]以便它可以评估 j 的值,然后搜索数组。 -
那么你是在告诉我它像矩阵一样工作吗?
标签: javascript arrays json api postman