使用空手道,如何通过JSON循环循环以基于JSON块中的另一个键来拾取键的值?

发布于 2025-01-26 11:47:17 字数 430 浏览 1 评论 0原文

以下是我想从中提取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚笙 2025-02-02 11:47:17

假设您已将X1存储在一个称为valx的变量中,则是从上一个呼叫中存储的。
内容获取Y1,

* def valY = karate.jsonPath(response, "$.result[?(@.x=='" + valX +"')]")[0].y

可以阅读

  • 现在,您可以使用以下 a href =“ https://stackoverflow.com/a/52397707/18026862”> https://stackoverflow.com/a/52397707/18026862
  • /空手道#jsonpath-filters“ rel =“ nofollow noreferrer”> https://github.com/karatelabs/karate#jsonpath-filters

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

* def valY = karate.jsonPath(response, "$.result[?(@.x=='" + valX +"')]")[0].y

You could read

白芷 2025-02-02 11:47:17

Yes, for dynamic JsonPath, please read the docs: https://github.com/karatelabs/karate #jsonpath-filters

在空手道的最新版本中1.0,我还建议使用JS做这样的事情:

* def xToFind = 'x1'
* def found = response.result.find(o => o.x == xToFind)
* match found == { x: 'x1', y: 'y1' } 

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:

* def xToFind = 'x1'
* def found = response.result.find(o => o.x == xToFind)
* match found == { x: 'x1', y: 'y1' } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文