0

我正在寻找为 json 对象中的所有字段打印 jsonpath 的 java API。我的第一部分要求是,对于给定的 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
        }
}

它应该以键值方式打印所有属性,其中键是字段的 jsonpath。

输出应如下所示 -

$.store.book[0].category=reference
$.store.book[0].author=Nigel Rees
$.store.book[0].title=Sayings of the Century
$.store.book[0].price=8.95
$.store.book[1].category=fiction
$.store.book[1].author=Evelyn Waugh
$.store.book[1].title=Sword of Honour
$.store.book[1].price=12.99

等等。这将用于匹配来自另一个 json 对象的值(我的第二个要求)。

我找到了一个 API https://github.com/json-path/JsonPath,它有点符合我的要求的第二部分,但没有任何实用程序方法或获取对象或字段的 JsonPath 的方法。

jsonpath 中是否有任何 java api 或方法,我可以在其中获取 json 对象中给定字段的 jsonpath?

4

1 回答 1

0

您是否尝试过使用 gson 库,请查看 com.google.gson.stream.JsonReader.getPath() 我认为这会让您朝着正确的方向前进。

于 2018-12-06T18:59:54.197 回答