我无法获取JSON对象的内部值

时间:2019-05-17 09:31:20

标签: karate

我无法获取此json对象的内部嵌套键的值(它从我们的API调用获取响应,我只复制了空手道的响应)


[
  {
    "code": 200,
    "result": "[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"
  }
]
And def tempreponse = [{"code":200,"result":"[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"}]

并且def temp1 = tempreponse [0] .result <-这句话有效 并且def temp1 = tempreponse [0] .result [0] <-但是,这不起作用,temp1的打印为空白

实际上,我需要获取personid和sampleid的值,但是失败了

1 个答案:

答案 0 :(得分:1)

是的,您的响应看起来确实不对,是JSON中的字符串以及全部。但是,即使这是预期的响应,空手道也可以应付。请参阅有关类型转换的文档:https://github.com/intuit/karate#type-conversion

* def response =
"""
[
  {
    "code": 200,
    "result": "[{\"distinct\":false,\"operatetime\":\"2019-05-17 17:01:01\",\"personid\":\"e8edec61-fd1a-4c69-8b60-fb8d21d06095\",\"sampleid\":\"1c9410cd-608d-4eb1-8d12-c8f2faf7fca4\"}]"
  }
]
"""
* json result = response[0].result
* def personId = result[0].personid
* match personId == 'e8edec61-fd1a-4c69-8b60-fb8d21d06095'