繁体   English   中英

POSTMAN:解析JSON响应,我可以深入到该响应中吗?

[英]POSTMAN : parsing a JSON response, how deep can I go into the response?

我目前正在尝试精确解析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:无法读取未定义的属性'type'”

使用控制台,我可以看到属性(执行console.log(body.entries [0] .attributes);),但无法更进一步。 这是邮递员的限制吗? 还有另一种方法可以精确地到达此device.type信息吗?

谢谢你的帮助

亚历山大

哎呀! 抱歉,我对此很陌生。 这是此问题的答案,可以通过这种方式访问​​device.type属性:body.entries [0] .attributes ['device.type']

但是有一些限制:-我不能使用.attributes [2],如果'attributes'可以看作是一个表,则2是元素的索引。 -语法特别,如果我们使用双引号而不是单引号,则该语法不起作用-我仍然不知道为什么在此阶段会发生这种情况...如果有人知道,我会很乐意阅读他

干杯

亚历山大

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM