arrays - 应用谓词后从 JSONPath 数组结果中获取特定对象

标签 arrays jsonpath

在 JSONPath 0.9.1 中,以下 Json 路径是有效的:

http://jsonpath.herokuapp.com/?path= $.store.book[?(@.author==%27Nigel%20Rees%27)][0]

回归

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

我已经升级到最新版本 (2.3),查询现在返回空数组。

这是错误还是从结果数组中检索元素的方式发生了变化?

最佳答案

鉴于此文档:

{
    "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
}

使用 JsonPath 2.3.0,以下代码返回 JSONArray(而不是 Object[]):

JsonPath.parse(JSON).read("$.store.book[?(@.author==\"Nigel Rees\")]");

所以,下面的代码...

JSONArray read = JsonPath.parse(JSON).read("$.store.book[?(@.author==\"Nigel Rees\")]");
System.out.println(read.get(0));

... 将打印:

{category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}

关于arrays - 应用谓词后从 JSONPath 数组结果中获取特定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47572081/

相关文章:

python - 在 numpy 数组的一维中跳过一个值

json - 如何使用 kubectl 在部署中修补容器环境变量?

java - Solr执行DataImportHandler时出现java.lang.ClassNotFoundException : net. minidev.json.writer.JsonReaderI

c# - Json.Net SelectToken 查询不起作用

arrays - 相对排序两个数组的最小交换

javascript - 如何在 Javascript 中有效地合并两个数组?

c - 在嵌入式 C 中查找最接近给定值的多维数组中的值

php - 将元素追加到数组并跳过某些值(如果存在)[PHP]

MongoDB 上的 JSONPath 查询?

键中点时的 JSONPath 语法