使用空手道,如何通过JSON循环循环以基于JSON块中的另一个键来拾取键的值?
以下是我想从中提取Y1的JSON响应。在得到响应之前,我已经知道X1,但我不知道Y1。因此,对于特定的X关键元素,我想获得其y值(在这种情况下为y1)。
我的JSON响应看起来像这样:
{
"result": [
{
"x": "x1",
"y": "y1"
},
{
"x": "x2",
"y": "y2"
}
]
}
下面是我在空手道中尝试的方法,但似乎取消了Null:
* def y = get[0] response..result[?(@.x=='x')]
我从以前的通话中捕获了X1。请让我知道我错了。 谢谢。
Below is the JSON response I get from which I want to extract y1. Before getting the response, I know x1 already but I don't know y1. So for that particular x key element, I want to get its y value (in this case y1).
My json response looks like this:
{
"result": [
{
"x": "x1",
"y": "y1"
},
{
"x": "x2",
"y": "y2"
}
]
}
Below is what I am trying in Karate but it seems it picks up null instead:
* def y = get[0] response..result[?(@.x=='x')]
I captured x1 from a previous call. Please let me know where I am wrong.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您已将X1存储在一个称为
valx
的变量中,则是从上一个呼叫中存储的。内容获取Y1,
可以阅读
Let's say you have stored x1 in a variable called
valX
from your previous call.You can now use the below to get y1
You could read
Yes, for dynamic JsonPath, please read the docs: https://github.com/karatelabs/karate #jsonpath-filters
在空手道的最新版本中1.0,我还建议使用JS做这样的事情:
Yes, for dynamic JsonPath, please read the docs: https://github.com/karatelabs/karate#jsonpath-filters
In the latest versions of Karate > 1.0, I also recommend using JS to do things like this: