首页 > 解决方案 > 如何在空手道中获取名称中包含@的JSON元素的值

问题描述

这是我的 json 回复

{
  "meta": {
 "type": "event-search",
 "@href": "https://<ip>:<port>/SentinelRESTServices/objects/event-search/10c21c52229bfe0545BD7802A74E10398534005056869AAA"
}
}

我想获得@href 的值。当我打印 meta.type 时,我可以获得值“事件搜索”,但同样不适用于 @href。

我试图使用 \\ (meta.\\@href) 来逃避它,但我得到了空值。

有没有办法逃避 @ 并获取值?

请帮忙!!

标签: karate

解决方案


当键名中有特殊字符时,请使用方括号 JS 方法提取嵌套属性。

例子:

* def response = { meta: { '@href': 'foo' } }
* match response.meta['@href'] == 'foo'

推荐阅读