在这里插入图片描述
四、表达式使用示例:
1、数据准备
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
}
五、在JAVA代码中使用
1、pom.xml文件中引入json-path的依赖
xml代码
< dependency>
< groupId>com.jayway.jsonpath</ groupId>
< artifactId>json-path< /artifactId>
< version>2.4.0< /version>
</ dependency>
2、编写代码
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JsonProvider;
public class JsonPathTest {
public static void main(String[] args) {
String json = readJson();
Configuration configuration = Configuration.defaultConfiguration();
configuration = configuration.addOptions(//
Option.DEFAULT_PATH_LEAF_TO_NULL, // 如果路径不存在则返回null,而不要抛出PathNotFoundException
Option.SUPPRESS_EXCEPTIONS // 抑制异常的抛出,当设置了Option.ALWAYS_RETURN_LIST时返回[],否则返回null
);
// 如果项目中存在gson则此处可以new 一个GsonJsonProvider,那么返回对象
// configuration.jsonProvider(new GsonJsonProvider());
JsonProvider jsonProvider = configuration.jsonProvider();
/**
* 此处预先解析json,默认请情下JsonPath.read方法每掉一次都会重新解析json,此处预先解析好就不用每次都进行解析
*/
Object document = jsonProvider.parse(json);
// 1.获取所有book中的author
List authors = JsonPath.read(document, “
.
s
t
o
r
e
.
b
o
o
k
[
∗
]
.
a
u
t
h
o
r
"
)
;
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
a
u
t
h
o
r
s
)
;
/
/
2.
获
取
价
格
比
.store.book[*].author"); System.out.println(authors); // 2.获取价格比
.store.book[∗].author");System.out.println(authors);//2.获取价格比.expensive大的书籍
List<Map<String, Object>> books = JsonPath.read(document, “$.store.book[?(@.price >
.
e
x
p
e
n
s
i
v
e
)
]
"
)
;
S
y
s
t
e
m
.
o
u
t
.
p
r
i
n
t
l
n
(
b
o
o
k
s
)
;
/
/
3.
输
出
一
个
不
存
在
的
表
达
式
,
因
为
存
在
这
个
选
项
的
配
置
,
所
有
返
回
的
是
O
p
t
i
o
n
.
D
E
F
A
U
L
T
P
A
T
H
L
E
A
F
T
O
N
U
L
L
O
b
j
e
c
t
n
o
t
E
x
i
s
t
s
V
a
l
u
e
=
J
s
o
n
P
a
t
h
.
u
s
i
n
g
(
c
o
n
f
i
g
u
r
a
t
i
o
n
)
.
p
a
r
s
e
(
d
o
c
u
m
e
n
t
)
.
r
e
a
d
(
"
.expensive)]"); System.out.println(books); // 3.输出一个不存在的表达式,因为存在这个选项的配置,所有返回的是Option.DEFAULT_PATH_LEAF_TO_NULL Object notExistsValue = JsonPath.using(configuration).parse(document).read("
.expensive)]");System.out.println(books);//3.输出一个不存在的表达式,因为存在这个选项的配置,所有返回的是Option.DEFAULTPATHLEAFTONULLObjectnotExistsValue=JsonPath.using(configuration).parse(document).read(".not-exists-path.path”);
System.out.println(notExistsValue);
// 4.存在多级属性缺失
Object object = JsonPath.using(configuration).parse(document).read(”$.not-exists.path");
System.out.println(object);
}
public static String readJson() {
StringBuilder builder = new StringBuilder();
try (InputStream is = JsonPathTest.class.getResourceAsStream("data.json"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr);{
String line;
while (null != (line = br.readLine())) {
builder.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
}