根据条件从响应中获取值,并将其存储到变量
我想根据条件从响应中获取值,然后将其存储到变量中。
在下面的JSON中,当名称与我喜欢的东西匹配时,我想存储该值。有没有方法可以使用空手道API实现这一目标?
{
"results": [
{
"name": "Sample1",
"email": "[email protected]",
"id": "U-123"
},
{
"name": "Sample2",
"email": "[email protected]",
"id": "U-456"
},
{
"name": "Sample3",
"email": "[email protected]",
"id": "U-789"
}
]
}
I would like to get the value from the response based on a condition and store it to a variable.
In the below JSON, I would like to store the value when the name matches to something I prefer. Is there a way to achieve this using Karate API?
{
"results": [
{
"name": "Sample1",
"email": "[email protected]",
"id": "U-123"
},
{
"name": "Sample2",
"email": "[email protected]",
"id": "U-456"
},
{
"name": "Sample3",
"email": "[email protected]",
"id": "U-789"
}
]
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,在阅读评论后,我的解释是“查找”
id
其中name
是sample2
。简单,只需使用filter()
操作,请参阅文档: https ://github.com/karatelabs/karate#jsonpath-filtersfind> find> find()方法作为非常简洁的示例
而不是使用过滤器,而是使用JS Array
一些时间了解其工作原理。与知道JS的人交谈。
So after reading the comment, my interpretation is to "find" the
id
wherename
isSample2
. Easy, just use afilter()
operation, refer the docs: https://github.com/karatelabs/karate#jsonpath-filtersInstead of using a filter, I'm using the JS Array
find()
method as a very concise example below:Take some time to understand how it works. Talk to someone who knows JS if needed.