空手道:JsonPath通配符不起作用或部分不起作用

问题描述 投票:1回答:1

JSON文件jsonExample

{
  "store": {
    "book": [
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "something": 12.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}

我想要更新"something"。我用的时候:

1)* set jsonExample $.store.book[0].something = 13 - 它正在运作

2)* set jsonExample $..book[0].something = 13 - 它正在运作

3)* eval jsonExample.store.book[0].something = 13 - 它正在运作

1)* set jsonExample $..something = 13 - 它不起作用

2)* eval jsonExample..something = 13 - 它不起作用

据我所知,set不使用通配符($[*].foo$..foo)。但是通配符是否与eval一起使用?如果有,怎么样?请基于上面的文件jsonExample的示例。

jsonpath karate
1个回答
2
投票

我不明白为什么你这么担心。通配符不适用于更新JSON。就这么简单。

还有一件事,eval将只使用纯JS。 Json-Path不是纯粹的JS。

也许这会更清楚地解释它。

如果* set jsonExample $..book[0].something = 13工作,请假设它是一个BUG。不要依赖它。它可能在这种情况下有效,因为代码尽可能具有弹性。但它可能不适用于其他情况或未来版本的空手道。

以下所有内容都有效:

* def jsonExample =
"""
{
  "store": {
    "book": [
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "something": 12.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}
"""
# all these will work
* set jsonExample $.store.book[0].something = 13
* match jsonExample.store.book[0].something == 13

* set jsonExample.store.book[0].something = 14
* match jsonExample.store.book[0].something == 14

* eval jsonExample.store.book[0].something = 15
* match jsonExample.store.book[0].something == 15

我真的希望这说清楚!!

© www.soinside.com 2019 - 2024. All rights reserved.