获取JSON密钥作为文本

时间:2019-07-12 19:38:51

标签: karate

尝试在空手道功能脚本中获取json键文本。

嗨,我是空手道的新手,正在研究空手道的所有文档。

当我得到Testing: 0, 1 Expected: 1.0 Result: 0.0 Testing: 1, 0 Expected: 1.0 Result: 0.0 Testing: 0, 0 Expected: 0.0 Result: 0.0 Testing: 1, 1 Expected: 0.0 Result: 0.0 响应时(如下面的代码所示),我不确定响应将包含哪些所有键。因此,只要响应中有关键文本为GET,那么我就想检索domain_name

domain_code

1 个答案:

答案 0 :(得分:1)

您在这里。如果任何部分不清楚,请阅读文档,从JsonPath开始:

* def response = 
"""
{
  "status": "SUCCESS",
  "totalCount": 1,
  "statusCode": "OK",
  "ResultData": {
    "data": [
        {"domain_code": "X3", "domain_name": "BMW" }, 
        {"domain_code": "Q5", "domain_name": "AUDI" }, 
        {"domain_code": "G450", "domain_name": "LEXUS" }
    ]
  }
}
"""
* def data = get[0] response..data[?(@.domain_name)]
* def keys = karate.keysOf(data)
* keys.remove('domain_name')
* print keys[0]