JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP.
jsonpath依赖
<json-path.version>2.3.0</json-path.version>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
jsonpath使用
List<String> authors = JsonPath.read(json, "$.store.book[*].author");
直接生成JsonNode
static {
Configuration.setDefaults(new Configuration.Defaults() {
private final JsonProvider jsonProvider = new JacksonJsonNodeJsonProvider();
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);
}
});
}
JsonNode authors = JsonPath.read(json, "$.store.book[*].author");
参考文档:
https://blog.csdn.net/koflance/article/details/63262484
网友评论