【问题标题】:Issue with filtering of JSON nodes with JsonPath使用 JsonPath 过滤 JSON 节点的问题
【发布时间】:2017-09-15 09:15:49
【问题描述】:

我有这种结构的 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")]

但我得到空结果,尽管根据同等的运营商文档,我希望我会收到“世纪语录”书节点:

"book1_ISBN": {
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": "8.95"
}

有趣的是,当我使用这个不带等号运算符的表达式时:

$[?(@.stores.*.books)].stores.*.books[?(@.*.category)]

我得到了所有三本书的结果。为什么我的等号表达式不起作用?

【问题讨论】:

    标签: java json jsonpath


    【解决方案1】:

    我在这里尝试了你的表达方式: http://jsonpath.com/? 并且它们都不起作用,带过滤器和不带过滤器。
    我不明白你为什么首先过滤这些元素:(@.stores.*.books)
    无论如何,如果您只想过滤所有类别为“小说”的书籍,这更简单:

    $..[?(@.category=="fiction")]
    

    希望对你有所帮助。

    【讨论】:

    • 我在这里测试了我的表达式:jsonpath.herokuapp.com 和没有等于运算符的表达式在那里工作,奇怪.. 不管怎样,我试试你的,它返回正确的结果(两个节点),谢谢,但我看到了结果不包含 ISBN 键 - 我怎么也能得到它?
    • 它确实...这就是我所看到的:
      [ { "category" : "fiction", "author" : "Evelyn Waugh", "title" : "Sword of Honor" ,“价格”:“12.99”},{“类别”:“小说”,“作者”:“J. R. R.托尔金”,“标题”:“指环王”,“isbn”:“0-395-19395 -8", "价格" : "22.99" } ]
    • 我的意思是“books”节点下的键,例如“book2_ISBN”、“book3_ISBN”
    • mmmh,恐怕你需要jsonPath中没有实现的父运算符:goessner.net/articles/JsonPath
    • 感谢您的推荐!
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2019-01-28
    • 2021-09-18
    • 1970-01-01
    相关资源
    最近更新 更多