【问题标题】:POSTMAN : parsing a JSON response, how deep can I go into the response?POSTMAN:解析 JSON 响应,我可以深入到响应中吗?
【发布时间】: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


【解决方案1】:

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

但有一些限制: - 如果“属性”可以被视为表格,我不能使用 .attributes[2], 2 作为元素的索引。 - 语法是特殊的,如果我们使用双引号而不是单引号,它就不起作用 - 我仍然不知道为什么会在这个阶段发生......如果有人知道,我很乐意阅读他的文章

干杯

亚历山大

【讨论】:

    猜你喜欢
    • 2018-12-04
    • 2016-05-01
    • 2021-07-02
    • 2021-08-04
    • 2013-03-27
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多