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
为例。
答案 0 :(得分:2)
我不明白你为什么这么担心。通配符不适用于更新 JSON。就这么简单。
还有另一件事,eval
仅适用于纯JS。 Json-Path是 NOT 纯JS。
也许这会更清楚地解释它。
如果* set jsonExample $..book[0].something = 13
在工作,请假定它是一个错误。不要依赖它。在这种情况下可能会起作用,因为代码尽可能具有弹性。但是在其他情况下或在空手道的未来版本中,它可能不起作用。
以下所有内容均可使用:
* 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
我真的希望这可以使它清楚!!