本文介绍了应用谓词后,从JSONPath数组结果中获取特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In JSONPath 0.9.1 the following Json path was valid:

$ .store.book [?(@。author ==%27Nigel%20Rees%27)] [0]

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),查询现在返回空数组。

I've upgraded to the latest one (2.3) and the query now returns empty array.

这是错误还是从结果数组中检索元素的方式已更改?

Is this a bug or the way to retrieve an element from the resulting array has changed?

推荐答案

给出文档:

{
    "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 [] ):

Using JsonPath 2.3.0, the following code returns a JSONArray (rather than an Object[]):

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

因此,以下代码...

So, the following code ...

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}

这篇关于应用谓词后,从JSONPath数组结果中获取特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:28