jsonpath-使⽤JSONPath解析JSON完整内容详解
JsonPath是⼀种简单的⽅法来提取给定JSON⽂档的部分内容。 JsonPath有许多编程语⾔,如Javascript,Python和PHP,Java。
JsonPath提供的json解析⾮常强⼤,它提供了类似正则表达式的语法,基本上可以满⾜所有你想要获得的json内容。下⾯我把官⽹介绍的每个表达式⽤代码实现,可以更直观的知道该怎么⽤它。
GitHub:
以下是从GitHub中⽤我蹩脚的英格利希,翻译过来的,将就着看吧,推荐去管⽹上⾯阅读。
JsonPath可在Central Maven存储库中到。 Maven⽤户将其添加到您的POM。
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
</dependency>
如果您需要帮助,请在中提问。标记问题'jsonpath'和'java'。
JsonPath表达式总是以与XPath表达式结合使⽤XML⽂档相同的⽅式引⽤JSON结构。
JsonPath中的“根成员对象”始终称为$,⽆论是对象还是数组。
JsonPath表达式可以使⽤点表⽰法
$.store.book [0].title
groupby函数python或括号表⽰法
$['store']['book'][0]['title']
操作符
操作说明
$查询根元素。这将启动所有路径表达式。
@当前节点由过滤谓词处理。
*通配符,必要时可⽤任何地⽅的名称或数字。
..深层扫描。必要时在任何地⽅可以使⽤名称。
.<name>点,表⽰⼦节点
['<name>' (, '<name>')]括号表⽰⼦项
[<number> (, <number>)]数组索引或索引
[start:end]数组切⽚操作
[?(<expression>)]过滤表达式。表达式必须求值为⼀个布尔值。
函数
函数可以在路径的尾部调⽤,函数的输出是路径表达式的输出,该函数的输出是由函数本⾝所决定的。
函数描述输出
min()提供数字数组的最⼩值Double
max()提供数字数组的最⼤值Double
avg()提供数字数组的平均值Double
stddev()提供数字数组的标准偏差值Double
length()提供数组的长度Integer
过滤器运算符
过滤器是⽤于筛选数组的逻辑表达式。⼀个典型的过滤器将是[?(@.age > 18)],其中@表⽰正在处理的当前项⽬。可以使⽤逻辑运算符&&和||创建更复杂的过滤器。字符串⽂字必须⽤单引号或双引号括起来([?(@.color == 'blue')] 或者 [?(@.color == "blue")]).
操作符描述
==left等于right(注意1不等于'1')
!=不等于
<⼩于
<=⼩于等于
>⼤于
>=⼤于等于
=~匹配正则表达式[?(@.name =~ /foo.*?/i)]
in左边存在于右边 [?(@.size in ['S', 'M'])]
nin左边不存在于右边
size(数组或字符串)长度
empty(数组或字符串)为空
Java操作⽰例
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
},
{
"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
}
JsonPath (点击链接测试)结果
获取json中store下book下的所有author值
获取所有json中所有author的值
所有的东西,书籍和⾃⾏车
60的补码怎么算获取json中store下所有price的值
获取json中book数组的第3个值
倒数的第⼆本书
前两本书
从索引0(包括)到索引2(排除)的所有图书
从索引1(包括)到索引2(排除)的所有图书
获取json中book数组的最后两个值
获取json中book数组的第3个到最后⼀个的区间值
获取json中book数组中包含isbn的所有值
获取json中book数组中price<10的所有值
获取json中book数组中price<=expensive的所有值
获取json中book数组中的作者以REES结尾的所有值(REES不区分⼤⼩写)
逐层列出json中的所有值,层级由外到内
获取json中book数组的长度
阅读⽂档
使⽤JsonPath的最简单的最直接的⽅法是通过静态读取API。
String json = "...";
List<String> authors = ad(json, "$.store.book[*].author");
如果你只想读取⼀次,那么上⾯的代码就可以了
如果你还想读取其他路径,现在上⾯不是很好的⽅法,因为他每次获取都需要再解析整个⽂档。所以,我们可以先解析整个⽂档,再选择调⽤路径。
String json = "...";
Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
String author0 = ad(document, "$.store.book[0].author");
String author1 = ad(document, "$.store.book[1].author");
JsonPath还提供流畅的API。这也是最灵活的⼀个。
String json = "...";
ReadContext ctx = JsonPath.parse(json);
List<String> authorsOfBooksWithISBN = ad("$.store.book[?(@.isbn)].author");
List<Map<String, Object>> expensiveBooks = JsonPath
.using(configuration)
.parse(json)
.read("$.store.book[?(@.price > 10)]", List.class);
何时返回
当在java中使⽤JsonPath时,重要的是要知道你在结果中期望什么类型。 JsonPath将⾃动尝试将结果转换为调⽤者预期的类型。
// 抛出 java.lang.ClassCastException 异常
List<String> list = JsonPath.parse(json).read("$.store.book[0].author")
// 正常
String author = JsonPath.parse(json).read("$.store.book[0].author")
当评估路径时,你需要理解路径确定的概念。路径是不确定的,它包含
..:深层扫描操作
(<expression>):表达式
[<number>, <number> (, <number>)]:多个数组索引
不确定的路径总是返回⼀个列表(由当前的JsonProvider表⽰)。
默认情况下,MappingProvider SPI提供了⼀个简单的对象映射器。这允许您指定所需的返回类型,MappingProvider将尝试执⾏映射。在下⾯的⽰例中,演⽰了Long和Date之间的映射。
String json = "{\"date_as_long\" : 1411455611975}";
Date date = JsonPath.parse(json).read("$['date_as_long']", Date.class);
如果您将JsonPath配置为使⽤JacksonMappingProvider或GsonMappingProvider,您甚⾄可以将JsonPath输出直接映射到POJO中。
Book book = JsonPath.parse(json).read("$.store.book[0]", Book.class);
要获取完整的泛型类型信息,请使⽤TypeRef。
TypeRef<List<String>> typeRef = new TypeRef<List<String>>(){};
List<String> titles = JsonPath.parse(JSON_DOCUMENT).read("$.store.book[*].title", typeRef);
谓词
在JsonPath中创建过滤器谓词有三种不同的⽅法。
内联谓词是路径中定义的谓词。
List<Map<String, Object>> books =  JsonPath.parse(json).read("$.store.book[?(@.price < 10)]");
你可以使⽤ && 和 || 结合多个谓词 [?(@.price < 10 && @.category == 'fiction')] , [?(@.category == 'reference' || @.price > 10)].
你也可以使⽤!否定⼀个谓词 [?(!(@.price < 10 && @.category == 'fiction'))].
过滤谓词
谓词可以使⽤Filter API构建,如下所⽰:
import static com.jayway.jsonpath.JsonPath.parse;
import static com.jayway.jsonpath.Criteria.where;
import static com.jayway.jsonpath.Filter.filter;
...
...
Filter cheapFictionFilter = filter(
where("category").is("fiction").and("price").lte(10D)
);
List<Map<String, Object>> books = parse(json).read("$.store.book[?]", cheapFictionFilter);
注意占位符?为路径中的过滤器。当提供多个过滤器时,它们按照占位符数量与提供的过滤器数量相匹配的顺序应⽤。您可以在⼀个过滤器操作[?,?]中指定多个谓词占位符,这两个谓词都必须匹配。
过滤器也可以与“OR”和“AND”
Filter fooOrBar = filter(
where("foo").exists(true)).or(where("bar").exists(true)
);
Filter fooAndBar = filter(
where("foo").exists(true)).and(where("bar").exists(true)
);
⾃定义
第三个选择是实现你⾃⼰的谓词
Predicate booksWithISBN = new Predicate() {
@Override
public boolean apply(PredicateContext ctx) {
return ctx.item(Map.class).containsKey("isbn");
}
};
List<Map<String, Object>> books = ad("$.store.book[?].isbn", List.class, booksWithISBN);
Path vs Value
在Goessner实现中,JsonPath可以返回Path或Value。值是默认值,上⾯所有⽰例返回。如果你想让我们查询的元素的路径可以通过选项来实现。
Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();
List<String> pathList = using(conf).parse(json).read("$..author");
assertThat(pathList).containsExactly(
"$['store']['book'][0]['author']", "$['store']['book'][1]['author']",
"$['store']['book'][2]['author']", "$['store']['book'][3]['author']");
调整配置
选项创建配置时,有⼏个可以改变默认⾏为的选项标志。
DEFAULT_PATH_LEAF_TO_NULL
此选项使JsonPath对于缺少的叶⼦返回null。考虑下⾯的json
[
python请求并解析json数据
{
"name" : "john",
"gender" : "male"
},
{
"name" : "ben"
}
]
Configuration conf = Configuration.defaultConfiguration();
// 正常
String gender0 = JsonPath.using(conf).parse(json).read("$[0]['gender']");
// 异常 PathNotFoundException thrown
String gender1 = JsonPath.using(conf).parse(json).read("$[1]['gender']");
Configuration conf2 = conf.addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
// 正常
String gender0 = JsonPath.using(conf2).parse(json).read("$[0]['gender']");
/
/ 正常 (返回 null)
String gender1 = JsonPath.using(conf2).parse(json).read("$[1]['gender']");
ALWAYS_RETURN_LIST
此选项配置JsonPath返回列表
Configuration conf = Configuration.defaultConfiguration();
// 正常
List<String> genders0 = JsonPath.using(conf).parse(json).read("$[0]['gender']");
// 异常 PathNotFoundException thrown
List<String> genders1 = JsonPath.using(conf).parse(json).read("$[1]['gender']");
SUPPRESS_EXCEPTIONS
该选项确保不会从路径评估传播异常。遵循这些简单的规则:
如果选项ALWAYS_RETURN_LIST存在,将返回⼀个空列表
如果选项ALWAYS_RETURN_LIST不存在返回null
JsonProvider SPI
JsonPath is shipped with three different JsonProviders:
(default)
更改配置默认值只能在应⽤程序初始化时完成。强烈不建议在运⾏时进⾏更改,尤其是在多线程应⽤程序中。
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() {
Of(Option.class);
}
});
请注意,JacksonJsonProvider需要com.:jackson-databind:2.4.5,GsonJsonProvider需要在您的类路径上使⽤de.gson:gson:2.3.1。
Cache SPI
在JsonPath 2.1.0中,引⼊了⼀种新的Cache SPI。这允许API消费者以适合其需求的⽅式配置路径缓存。缓存必须在⾸次访问之前配置,或者抛出JsonPathException。 JsonPath
附带两个缓存实现
com.jayway.jsonpath.spi.cache.LRUCache(默认,线程安全)
com.jayway.jsonpath.spi.cache.NOOPCache(⽆缓存)
如果要实现⾃⼰的缓存,API很简单。
CacheProvider.setCache(new Cache() {
//Not thread safe simple cache
private Map<String, JsonPath> map = new HashMap<String, JsonPath>();
@Override
public JsonPath get(String key) {
(key);
}
@Override
public void put(String key, JsonPath jsonPath) {
map.put(key, jsonPath);
}
});
Java操作⽰例源码
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},{"category":"fiction","author":"Herman Melville","title"
Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.List;
import com.jayway.jsonpath.JsonPath;
public class TestJsonPath {
public static void main(String[] args) {
String sjson = readtxt();
print("--------------------------------------getJsonValue--------------------------------------");
getJsonValue(sjson);
}
private static String readtxt() {
StringBuilder sb = new StringBuilder();
try {
FileReader fr = new FileReader("D:/");
BufferedReader bfd = new BufferedReader(fr);
String s = "";
while((adLine())!=null) {
sb.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.String());
String();
}
private static void getJsonValue(String json) {
//The authors of all books:获取json中store下book下的所有author值
List<String> authors1 = ad(json, "$.store.book[*].author");
//All authors:获取所有json中所有author的值
List<String> authors2 = ad(json, "$..author");
//All things, both books and bicycles
//authors3返回的是net.minidev.json.JSONArray:获取json中store下的所有value值,不包含key,如key有两个,book和bicycle
List<Object> authors3 = ad(json, "$.store.*");
//The price of everything:获取json中store下所有price的值
List<Object> authors4 = ad(json, "$.store..price");
//The third book:获取json中book数组的第3个值
List<Object> authors5 = ad(json, "$..book[2]");
//The first two books:获取json中book数组的第1和第2两个个值
List<Object> authors6 = ad(json, "$..book[0,1]");
//All books from index 0 (inclusive) until index 2 (exclusive):获取json中book数组的前两个区间值
斗地主小程序开发需要多少钱List<Object> authors7 = ad(json, "$..book[:2]");
//All books from index 1 (inclusive) until index 2 (exclusive):获取json中book数组的第2个值
List<Object> authors8 = ad(json, "$..book[1:2]");
//Last two books:获取json中book数组的最后两个值
List<Object> authors9 = ad(json, "$..book[-2:]");
//Book number two from tail:获取json中book数组的第3个到最后⼀个的区间值
List<Object> authors10 = ad(json, "$..book[2:]");
//All books with an ISBN number:获取json中book数组中包含isbn的所有值
List<Object> authors11 = ad(json, "$..book[?(@.isbn)]");
//All books in store cheaper than 10:获取json中book数组中price<10的所有值
List<Object> authors12 = ad(json, "$.store.book[?(@.price < 10)]");
/
/All books in store that are not "expensive":获取json中book数组中price<=expensive的所有值
List<Object> authors13 = ad(json, "$..book[?(@.price <= $['expensive'])]");
//All books matching regex (ignore case):获取json中book数组中的作者以REES结尾的所有值(REES不区分⼤⼩写)
List<Object> authors14 = ad(json, "$..book[?(@.author =~ /.*REES/i)]");
//Give me every thing:逐层列出json中的所有值,层级由外到内
List<Object> authors15 = ad(json, "$..*");
//The number of books:获取json中book数组的长度
List<Object> authors16 = ad(json, "$..book.length()");
print("**********authors1**********");
print(authors1);
menustrip控件代码应用
print("**********authors2**********");
print(authors2);
print("**********authors3**********");
printOb(authors3);
print("**********authors4**********");
printOb(authors4);
print("**********authors5**********");
printOb(authors5);
print("**********authors6**********");
printOb(authors6);
print("**********authors7**********");
printOb(authors7);
print("**********authors8**********");
printOb(authors8);
print("**********authors9**********");
printOb(authors9);
print("**********authors10**********");
printOb(authors10);
print("**********authors11**********");
printOb(authors11);
print("**********authors12**********");
printOb(authors12);
print("**********authors13**********");
printOb(authors13);
print("**********authors14**********");
printOb(authors14);
sql在insert如何验证
print("**********authors15**********");
printOb(authors15);
print("**********authors16**********");
printOb(authors16);
}
private static void print(List<String> list) {
for(Iterator<String> it = list.iterator();it.hasNext();) {
System.out.());
}
}
private static void printOb(List<Object> list) {
for(Iterator<Object> it = list.iterator();it.hasNext();) {
System.out.());
}
}
private static void print(String s) {
System.out.println("\n"+s);
}
}
输出
{"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" --------------------------------------getJsonValue--------------------------------------
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/apache/logging/log4j/log4j-slf4j-impl/2.0.2/log4j-slf4j-impl-2.0.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/workSpaces/SupportPackge/MavenRepository/org/slf4j/slf4j-log4j12/1.7.10/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See /codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
**********authors1**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors2**********
Nigel Rees
Evelyn Waugh
Herman Melville
X-rapido
J. R. R. Tolkien
**********authors3**********
[{"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","is {color=red, price=19.95}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。