我有这样结构的 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")]
但是我得到空结果,尽管根据相等运算 rune 档我希望我会收到“世纪俗语”书籍节点:
"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)
无论如何,如果您只想过滤所有类别为“小说”的书籍,这更简单:
$..[?(@.category=="fiction")]
我希望这会有所帮助。
关于java - 使用 JsonPath 过滤 JSON 节点的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236012/