Jsonpath与杰克逊或Gson [英] Jsonpath with Jackson or Gson
问题描述
我得到一个大的json文档,我只想解析它的一部分到我的java类。我正在考虑使用诸如jsonpath之类的东西来从中提取部分数据,而不是创建完整的java类层次结构。
杰克逊或Gson以任何方式支持jsonpath吗?如果是的话,你能给我提供一些例子或指向另一个标准库用于这个目的吗?例如让我说我有一个下面的文档,我想只提取下面的数据在我的Java类:
$。store.book [0] - 只有第一本书
$ .store.bicycle.price - price of自行车
{
store:{
book:[
{
category:reference,
author:Nigel Rees,
title:世纪之语,
price:8.95
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b价格:12.99
},
{
类别:小说,
作者:赫尔曼梅尔维尔,
title :Moby Dick,
isbn:0-553-21311-3,
price:8.99
},
{
category:fiction,
作者:J. RR Tolkien,
title:指环王,
isbn:0-395-19395-8,
价格:22.99
bicycle:{
color:red,
price:19.95
}
},
expensive:10
}
Jayway JsonPath 库支持使用JSON路径读取值。
例如:
字符串json =...;
Map< String,Object> book = JsonPath.read(json,$ .store.book [0]);
System.out.println(book); //打印{category = reference,author = Nigel Rees,title =世纪之言,价格= 8.95}
双倍价格= JsonPath.read(json,$ .store.bicycle.price);
System.out .println(price); //打印19.95
您可以还可以将JSON对象直接映射到类,如GSON或Jackson:
$ b $ pre $ Book book = JsonPath.parse(json).read( $ .store.book [0],Book.class);
System.out.println(book); // print book {category ='reference',author ='Nigel Rees',title ='世纪的谚语',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);
}
});
请参阅文档了解更多详情。
I am getting a big json document and i want to parse only some part of it to my java classes. I was thinking to use something like jsonpath to extract partial data from it instead of creating entire hierarchy of java classes.
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
You can also map JSON objects directly to classes, like in GSON or 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}
If you would like to specifically use GSON or Jackson to do the deserialization (the default is to use json-smart), you can also configure this:
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);
}
});
See the documentation for more details.
这篇关于Jsonpath与杰克逊或Gson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!