Java 使用JsonPath过滤JSON节点的问题
我有这样的JSON结构:Java 使用JsonPath过滤JSON节点的问题,java,json,jsonpath,Java,Json,Jsonpath,我有这样的JSON结构: { "stores": { "store1_id": { "books": { "book1_ISBN": { "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century
{
"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"
}
}
}
}
}
我用语法分析它。我尝试使用以下表达式获取所有小说类书籍:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category=="reference")]
但我得到的结果是空的,尽管根据equal运算符文档,我希望我将收到“世纪格言”书籍节点:
"book1_ISBN": {
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": "8.95"
}
有趣的是,我使用这个表达式时没有equals运算符:
$[?(@.stores.*.books)].stores.*.books[?(@.*.category)]
结果我得到了所有三本书。为什么我使用equal运算符的表达式不起作用?我在这里尝试了您的表达式: ? 无论有无过滤器,它们都不起作用。
我不明白为什么要先过滤这些元素:(@.stores.*.books)
无论如何,如果您只想过滤所有类别为“小说”的书籍,这更简单:
$..[?(@.category=="fiction")]
我希望这能有所帮助。我在这里测试了我的表达式:没有equals运算符的表达式在那里工作,奇怪。。无论如何,我尝试了你们的一个,它返回正确的结果(两个节点),谢谢,但我看到结果不包含ISBN键-我怎么能得到它呢?它有。。。这就是我所看到的:
[{“类别”:“小说”、“作者”:“伊芙琳·沃”、“头衔”:“荣誉之剑”、“价格”:“12.99”},{“类别”:“小说”、“作者”:“J.R.R.托尔金”、“头衔”:“指环王”、“isbn”:“0-395-19395-8”、“价格”:“22.99”}]我指的是“books”节点下的键,例如“book2_-ISBN”、“book3_-ISBN”mmmh,恐怕您需要在jsonPath中未实现的父运算符:谢谢您的建议!