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
的示例。
我不明白为什么你这么担心。通配符不适用于更新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
我真的希望这说清楚!!