简体   繁体   English

如何为json对象打印jsonpath

[英]How to print jsonpath for json object

I am looking for java API which prints jsonpath for all fields in json object. 我正在寻找为json对象中所有字段输出jsonpath的java API。 My 1st part of requirement is like, for given json object (or string) - 我的需求的第一部分就像对于给定的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
        }
}

it should print all properties in key-value manner where key is jsonpath of the field. 它应该以键值方式打印所有属性,其中key是字段的jsonpath。

the output should look something like below - 输出应如下所示-

$.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

and so on. 等等。 this will be used to match values from another json object (my 2nd requirement). 这将用于匹配另一个json对象的值(我的第二个要求)。

I found an API https://github.com/json-path/JsonPath which is somewhat fits to 2nd part of my requirement but is does not have any utility method or a method to get JsonPath for a object or field. 我找到了一个API https://github.com/json-path/JsonPath ,它在某种程度上适合我的要求的第二部分,但是没有任何实用程序方法或获取对象或字段的JsonPath的方法。

Is there any java api or method in jsonpath where I can get jsonpath for given field in json object? jsonpath中是否有任何Java API或方法,我可以在其中获取json对象中给定字段的jsonpath?

您是否尝试过使用gson库,请看一下com.google.gson.stream.JsonReader.getPath(),我认为这将为您提供正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM