首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json-path:数组过滤器结果中的第一个元素

json-path:数组过滤器结果中的第一个元素
EN

Stack Overflow用户
提问于 2018-08-06 19:54:09
回答 1查看 2.7K关注 0票数 0

我刚接触json-path,但我经常使用xpath。我的问题是用json-path提取前一个过滤器产生的数组的nth元素。例如,使用以下json

代码语言:javascript
复制
{
    "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
            },
            {
                "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
}

我想将第一本书与“小说”类别相匹配。

我试着用

代码语言:javascript
复制
$..book[?(@.category=='fiction')][0]

但似乎它与任何在线评估工具都不起作用。

错误在哪里?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-09 18:36:28

您可以尝试将books数组映射到满足条件的列表中,并从中获取nth元素。像这样:

代码语言:javascript
复制
List<Map<String, Object>> bookList = JsonPath.parse(your_json).read("$..book[?(@.category=='fiction')]", List.class);
System.out.println("Printing the nth book = "+expensiveBooks.get(n));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51707036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档