如何使用 JSONPath 过滤单个字符串?

How to filter single string using JSONPath?

我有以下 JSON 结构:

    [ 
        { "category": "reference",
          "author": "Nigel Rees",
          "title": "Sayings of the Century",
          "price": 8.95
        },
        { "category": "fiction",
          "author": "Evelyn Waugh",
          "title": "Sword of Honour",
          "price": 12.99
        },
        { "category": "fiction",
          "author": "Herman Melville",
          "title": "Moby Dick",
          "isbn": "0-553-21311-3",
          "price": 8.99
        },
        { "category": "fiction",
          "author": "J. R. R. Tolkien",
          "title": "The Lord of the Rings",
          "isbn": "0-395-19395-8",
          "price": 22.99
        }
    ]

我只想得到一个 object 包含标题“指环王”

如果我输入:

$.[*].title

它给我的输出是:

[
 "Sayings of the Century",
 "Sword of Honour",
 "Moby Dick",
 "The Lord of the Rings"
]

我试过这样做:

$.[*].[?(@.title=='The Lord of the Rings')]

但是没有用。 拜托,有人可以帮助我吗?

提前致谢!

请原谅我之前的回答。我不知道 jsonpath 是一回事。我找到了你的答案:

$[?(@.title == 'The Lord of the Rings')]

原因是,由于 objects 在根元素中,您已经在尝试 select 带有 @.title 的元素中的标题。当您 select 所有带有 $.[*] 的元素时,您的级别已经太低,无法按标题查找。