Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

karate - I cannot get the inner value of a JSON object

I cannot get the inner nested key's value of this json object (it gets response from our API call, I just only copy the response from karate)


[
  {
    "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"}]"}]

And def temp1 = tempreponse[0].result <- this sentence works And def temp1 = tempreponse[0].result[0] <- however, this doesn't work, the print of temp1 is blank

In fact, I need to get the value of personid and sampleid, but failed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yeah your response looks really wrong, a string within a JSON and all. But even if that is the expected response, Karate can handle it. Refer the documentation on type conversions: 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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...