【发布时间】:2017-11-07 07:11:19
【问题描述】:
我目前正在尝试精确解析 JSON 响应(使用 POSTMAN)。
JSON 响应具有以下结构(我使用 ... 来跳过不相关的信息):
*
{
"href": ...,
"offset": ...,
"limit": ...,
"first": ...,
"last": ...,
"entries": [
{
"href": ...,
"id": ...,
"name": "MY_FIRST_ITEM_NAME",
"tags": [
...,
...
],
"objectClass": [
...
],
"attributes": {
...,
...,
...,
"device.type": "MY_ITEM_TYPE,
...
},
...
},
{
"href": ...,
"id": ...,
"name": "MY_SECOND_ITEM_NAME",
"tags": [
...,
...
],
"objectClass": [
...
],
"attributes": {
...,
...,
...,
"device.type": "MY_ITEM_TYPE,
...
},
...
},
...
]
}*
我想测试几个已知值: tests["test first item"] = responseBody.has("MY_FIRST_ITEM_NAME") 这可行,但我也想检查相关的设备类型 如果我使用 responseBody.has("MY_ITEM_TYPE") 我无法弄清楚它与哪个项目相关,所以我尝试进行更精确的检查: *
tests["test entries 0"] = body.entries[0].name === "MY_FIRST_ITEM_NAME"*;
this works but when it comes to test the device type:
tests["test entries 0"] = body.entries[0].attributes.device.type
最终出现错误“TypeError: Cannot read property 'type' of undefined”
使用控制台,我可以看到属性(执行 console.log(body.entries[0].attributes);),但不可能更深入。这是邮递员的限制吗?是否有另一种方法可以准确获取此 device.type 信息?
感谢您的帮助
亚历山大
【问题讨论】:
-
从显示响应的响应字段的下拉列表中选择 JSON
-
其实有人帮我解决了这个问题。答案是:body.entries[0].attributes['device.type'] 返回值。注意引号,它只适用于单曲。
标签: javascript json postman