【问题标题】:Karate - Exception raises for invalid jsonpath空手道 - 无效 jsonpath 引发异常
【发布时间】:2020-02-14 17:02:55
【问题描述】:

我有一个如下的 Json 响应。这里的区别是我的 Json body 有一个数字作为父节点。

    def response =
"""
{
  "22388043":[
    {
      "firstName":"Romin",
      "lastName":"Irani",
      "phoneNumber":"408-1234567",
      "emailAddress":"romin.k.irani@gmail.com"
    }
  ]
}
"""

我想从响应正文中返回 mobileNumber 属性值。在这种情况下,我的回复中没有该属性。所以这里我想得到一个空值。

所以当我使用 * def mobile = $.22388043[0].mobileNumber 时,我遇到了错误。

路径没有结果:$['22388043'][0]['mobileNumber']

请就此提出建议。

【问题讨论】:

  • 我没有看到任何名为 mobileNumber 的属性,我想您的意思是 phoneNumber?
  • 在这个回复中,我没有 mobileNumber 属性。当该属性存在时,值将返回。我遇到的问题是当属性不存在时我收到此错误而不是获得空值
  • * def mobileNum = karate.get("$.22388043[0].mobileNumber")

标签: karate


【解决方案1】:

空手道确实为您提供了一种获取 JSON 键值的方法。

希望这个示例也能回答您的所有其他问题:

* def response =
"""
{
  "22388043":[
    {
      "firstName":"Romin",
      "lastName":"Irani",
      "phoneNumber":"408-1234567",
      "emailAddress":"romin.k.irani@gmail.com"
    }
  ]
}
"""
* def id = karate.keysOf(response)[0]
* match id == '22388043'
* def person = response[id][0]
* match person contains { firstName: 'Romin', lastName: 'Irani' }
* match person.mobileNumber == '#notpresent'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多