我得到一个很大的json文档,我只想将其中的一部分解析到我的java类中.我想用jsonpath之类的东西从中提取部分数据,而不是创建整个java类层次 struct .

Does Jackson or Gson support jsonpath in any way? If yes, can you please provide me some examples or point to another standard library for this purpose?

For example lets say i have a below document and i want to extract only below data from it in my java classes:

$.store.book[0] - Only first book $.store.bicycle.price - price of bicycle

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

推荐答案

The Jayway JsonPath library has support for reading values using a JSON path.

For example:

String json = "...";

Map<String, Object> book = JsonPath.read(json, "$.store.book[0]");
System.out.println(book);  // prints {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95}

Double price = JsonPath.read(json, "$.store.bicycle.price");
System.out.println(price);  // prints 19.95

您还可以将JSON对象直接映射到类,如GSON或Jackson中:

Book book = JsonPath.parse(json).read("$.store.book[0]", Book.class);
System.out.println(book);  // prints Book{category='reference', author='Nigel Rees', title='Sayings of the Century', price=8.95}

如果您想专门使用gson或Jackson进行反序列化(默认使用json-Smart),您也可以这样配置:

Configuration.setDefaults(new Configuration.Defaults() {
    private final JsonProvider jsonProvider = new JacksonJsonProvider();
    private final MappingProvider mappingProvider = new JacksonMappingProvider();

    @Override
    public JsonProvider jsonProvider() {
        return jsonProvider;
    }

    @Override
    public MappingProvider mappingProvider() {
        return mappingProvider;
    }

    @Override
    public Set<Option> options() {
        return EnumSet.noneOf(Option.class);
    }
});

更多细节见the documentation.

Json相关问答推荐

PowerShell脚本-替换json数组(转义$var将被视为不带双引号的文本)

使用Jolt v.0.1.1向每个json元素添加一个键-值对

无法从MongoDB集合中检索正确的文档

bash用jq获取第二条JSON记录

删除 JOLT 中的方括号

如何对未知/变量键的字典进行编码?

Spark-SQL中的from_unixtime函数未能给出正确的输出

Bicep脚本中如何设置弹性池的维护窗口?

使用 Groovy 将 XML 转换为 JSON

VBA-JSON 嵌套集合解析为 Excel

如何使用 Google 表格应用程序脚本将 JSON 中的多个字段提取到 Google 表格中

jq:来自嵌套 JSON 的映射

判断 JSON 中的对象,而不是条件中提到的对象

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

Android 上的 JSON - 序列化

将错误消息作为 JSON 对象发送

在 JSON.stringify() 的输出中隐藏空值

Python 到 JSON 序列化在十进制上失败

Volley JsonObjectRequest Post 参数不再起作用

Java HashMap 与 JSONObject