1,json数据和POJO的关系
目前微服务非常火,前后端分离成为主流趋势,接口返回的数据主要以json数据为主。后端研发需要将POJO转为json字符串。微服务之间的调用或调用第三方接口返回的json数据需要转为POJO对象进行逻辑处理。
目前使用的比较多的json和POJO互转的工具包有:
FastJson (每年都要爆出多次漏洞)
FastJson2
Gson
Jackson (Spring内置)
Hutool
json-lib
JsonPath (本次主角)
2,JsonPath返回结果
在java中使用JsonPath时,知道返回的结果类型是很重要的。JsonPath将自动尝试将结果强制转换为调用方所期望的类型。
//Will throw an java.lang.ClassCastException
List<String> list = JsonPath.parse(json).read("$.store.book[0].author")
//Works fine
String author = JsonPath.parse(json).read("$.store.book[0].author")
计算路径时需要知道路径是明确的还是不明确的,明确的返回单个值,不明确的返回一个List.
不明确的路径有如下几种情况:
..
- 深度扫描操作?(<expression>)
- 表达式[<number>, <number> (, <number>)]
- 多个数组索引
3,JsonPath提供的MappingProvider和JsonProvider
默认情况下,一个简单的对象映射器由MappingProvider SPI提供。JsonPath提供了默认的实现: JsonSmartJsonProvider
和 JsonSmartMappingProvider
。但是不能将json字符串转为POJO对象。
JsonPath提供的MappingProvider如下:
- JsonSmartMappingProvider(默认)
- GsonMappingProvider
- JacksonMappingProvider
- JakartaMappingProvider
- JsonOrgMappingProvider
- TapestryMappingProvider
JsonPath提供的JsonProvider如下:
- JsonSmartJsonProvider (默认)
- GsonJsonProvider
- JacksonJsonNodeJsonProvider
- JacksonJsonProvider
- JakartaJsonProvider
- JsonOrgJsonProvider
- TapestryJsonProvider
4,JsonPath配置,支持json转POJO
在初始化应用程序时才能更改所配置默认值。强烈反对在运行时进行更改,尤其是在多线程应用程序中。
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);
}
});
注意:
JacksonJsonProvider 要求 com.fasterxml.jackson.core:jackson-databind:2.4.5
GsonJsonProvider 要求 com.google.code.gson:gson:2.3.1
5,实战
准备数据
{
"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
}
准备POJO对象
/**
* 书籍实体
*/
@Data
public static class Book {
/**
* 分类
*/
private String category;
/**
* 作者
*/
private String author;
/**
* 标题
*/
private String title;
/**
* 价格
*/
private Double price;
/**
* 书籍编号
*/
private String isbn;
}
@Data
public static class Bicycle {
/**
* color
*/
private String color;
/**
* price
*/
private Double price;
}
@Data
public static class Store {
/**
* book
*/
private List<Book> book;
/**
* bicycle
*/
private Bicycle bicycle;
}
@Data
public static class Demo {
/**
* store
*/
private Store store;
/**
* expensive
*/
private Integer expensive;
}
maven 坐标
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.7.0</version>
</dependency>
<!-- json-path 配置MappingProvider -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
5.1,json转Book
代码:
/**
* json字符串转为单个pojo对象
*/
@Test
public void json2Pojo(){
Configuration.setDefaults(new Configuration.Defaults() {
@Override
public MappingProvider mappingProvider() {
// 需要引入 jackson-databind jar
return new JacksonMappingProvider();
}
@Override
public JsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
});
Book book = JsonPath.parse(jsonData).read("$.store.book[0]", Book.class);
Console.log(JSONUtil.toJsonStr(book));
}
输出:
{"author":"Nigel Rees","title":"Sayings of the Century","price":8.95,"category":"reference"}
5.2,json转List
代码:
/**
* json字符串转为List
*/
@Test
public void json2List(){
Configuration.setDefaults(new Configuration.Defaults() {
@Override
public MappingProvider mappingProvider() {
// 需要引入 jackson-databind jar
return new JacksonMappingProvider();
}
@Override
public JsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
});
TypeRef<List<Book>> typeRef = new TypeRef<List<Book>>(){};
List<Book> books = JsonPath.parse(jsonData).read("$..book[*]", typeRef);
Console.log(JSONUtil.toJsonStr(books));
}
输出:
[{"author":"Nigel Rees","title":"Sayings of the Century","price":8.95,"category":"reference"},{"author":"Evelyn Waugh","title":"Sword of Honour","price":12.99,"category":"fiction"},{"author":"Herman Melville","isbn":"0-553-21311-3","title":"Moby Dick","price":8.99,"category":"fiction"},{"author":"J. R. R. Tolkien","isbn":"0-395-19395-8","title":"The Lord of the Rings","price":22.99,"category":"fiction"}]
5.3,json转复杂POJO
代码:
/**
* json字符串转复杂的pojo
*/
@Test
public void json2ComplexPojo(){
Configuration.setDefaults(new Configuration.Defaults() {
@Override
public MappingProvider mappingProvider() {
// 需要引入 jackson-databind jar
return new JacksonMappingProvider();
}
@Override
public JsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
});
TypeRef<Demo> typeRef = new TypeRef<Demo>(){};
// $ 代表整个json字符串
Demo demo = JsonPath.parse(jsonData).read("$", typeRef);
Console.log(JSONUtil.toJsonStr(demo));
}
输出:
{"store":{"bicycle":{"color":"red","price":19.95},"book":[{"author":"Nigel Rees","title":"Sayings of the Century","price":8.95,"category":"reference"},{"author":"Evelyn Waugh","title":"Sword of Honour","price":12.99,"category":"fiction"},{"author":"Herman Melville","isbn":"0-553-21311-3","title":"Moby Dick","price":8.99,"category":"fiction"},{"author":"J. R. R. Tolkien","isbn":"0-395-19395-8","title":"The Lord of the Rings","price":22.99,"category":"fiction"}]},"expensive":10}
6,总结
- json转嵌套多或复杂的POJO,使用TypeRef。
- JsonPath可以结合断言实现对json数据过滤。
- JsonPath返回的List数据是按json字符串出现的顺序返回,即有序的。