gpt4 book ai didi

java - JSON PATH 字段 NULL 检查表达式

转载 作者:行者123 更新时间:2023-12-02 06:16:04 27 4
gpt4 key购买 nike

我有一个如下所示的 json 数组:

{
"store": {
"book": [
{
"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,
"likes": 1
},
{
"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
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10}

我试图找到所有没有喜欢字段的图书价格。是否可以使用 Jayway Json Path library 找到。我正在尝试这样的 $.store.book[? (@.likes == null)].price 但我不知道是否有类似的东西?

最佳答案

这个 jsonpath 表达式是正确的:

$.store.book[?(@.likes == null)].price

但您必须设置DEFAULT_PATH_LEAF_TO_NULL,例如:

Configuration conf = Configuration.defaultConfiguration()
.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

String pricesForBooksWithNoLikes = JsonPath.using(conf).parse(json)
.read("$.store.book[?(@.likes == null)].price");

更多详情in the docs .

这是显示此读取工作的屏幕截图,使用 Jayway JsonPath Evaluator :

enter image description here

关于java - JSON PATH 字段 NULL 检查表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109347/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com