使用 JsonPath 过滤 JSON 个节点的问题
Issue with filtering of JSON nodes with JsonPath
我有 JSON 这样的结构:
{
"stores": {
"store1_id": {
"books": {
"book1_ISBN": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": "8.95"
},
"book2_ISBN": {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": "12.99"
}
}
},
"store2_id": {
"books": {
"book3_ISBN": {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": "22.99"
}
}
}
}
}
我用 Jayway JsonPath 解析它。我尝试使用这样的表达式获取所有小说类别的书籍:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category=="reference")]
但我得到的结果是空的,尽管根据 equal 操作员文档,我希望我会收到 "Sayings of the Century" 书节点:
"book1_ISBN": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": "8.95"
}
有趣的是,当我使用不带等号运算符的表达式时:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category)]
我得到了所有三本书的结果。为什么我的等号运算符表达式不起作用?
我在这里试过你的表达方式:
http://jsonpath.com/?
而且它们都不起作用,无论是带过滤器还是不带过滤器。
我不明白你为什么要先过滤这些元素:(@.stores.*.books)
无论如何,如果你只是想过滤类别为"fiction"的所有书籍,这更简单:
$..[?(@.category=="fiction")]
希望对您有所帮助。
我有 JSON 这样的结构:
{
"stores": {
"store1_id": {
"books": {
"book1_ISBN": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": "8.95"
},
"book2_ISBN": {
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": "12.99"
}
}
},
"store2_id": {
"books": {
"book3_ISBN": {
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": "22.99"
}
}
}
}
}
我用 Jayway JsonPath 解析它。我尝试使用这样的表达式获取所有小说类别的书籍:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category=="reference")]
但我得到的结果是空的,尽管根据 equal 操作员文档,我希望我会收到 "Sayings of the Century" 书节点:
"book1_ISBN": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": "8.95"
}
有趣的是,当我使用不带等号运算符的表达式时:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category)]
我得到了所有三本书的结果。为什么我的等号运算符表达式不起作用?
我在这里试过你的表达方式:
http://jsonpath.com/?
而且它们都不起作用,无论是带过滤器还是不带过滤器。
我不明白你为什么要先过滤这些元素:(@.stores.*.books)
无论如何,如果你只是想过滤类别为"fiction"的所有书籍,这更简单:
$..[?(@.category=="fiction")]
希望对您有所帮助。