书接上文:JsonPath实践(一)

本期聊一下如何使用​​JSonpath​​​标记语法处理,​​json​​对象中的数组主要内容是提取数组中对象和对象集合。

json数据

首先看官方给的​​json​​​数据的​​Demo​​(我做了一点点修改):

JSONObject json = JSON.parseObject("{" +
" \"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," +
" \"ss\": [32,32,4,23]" +
"}");

获取数组中的有序对象

​jsonpath​​​:​​$.store.book[2]​

  • 这里注意索引是从0开始的。

​jsonpath​​​:​​$.store.book[-1]​

  • 这里表示倒数第一个对象

代码:

Object read = JsonPath.read(json, "$.store.book[2]");
output(JSON.parseObject(read.toString()));

等效写法:

JSONObject read = JsonPath.read(json, "$.store.book[2]");
output(read);

控制台输出:

INFO-> 当前用户:fv,IP:10.60.131.54,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO->
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {
> ① . "author":"Herman Melville",
> ① . "price":8.99,
> ① . "isbn":"0-553-21311-3",
> ① . "category":"fiction",
> ① . "title":"Moby Dick"
> }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

获取数组中的有序对象切片

  • 这里的语法类似于​​Python​​中对于数组的处理。

​jsonpath​​​:​​$.store.book[:2]​

  • 倒数截取

​jsonpath​​​:​​$.store.book[-2:]​

代码:

Object read = JsonPath.read(json, "$.store.book[2]");
output(JSON.parseObject(read.toString()));

等效写法省略……

控制台输出:

INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO->
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {
> ① . "author":"Nigel Rees",
> ① . "price":8.95,
> ① . "category":"reference",
> ① . "title":"Sayings of the Century"
> }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO->
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {
> ① . "author":"Evelyn Waugh",
> ① . "price":12.99,
> ① . "category":"fiction",
> ① . "title":"Sword of Honour"
> }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0
  • 倒数截取
INFO-> 当前用户:fv,IP:10.60.192.21,工作目录:/Users/fv/Documents/workspace/fun/,系统编码格式:UTF-8,系统Mac OS X版本:10.15.6
INFO->
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {
> ① . "author":"Herman Melville",
> ① . "price":8.99,
> ① . "isbn":"0-553-21311-3",
> ① . "category":"fiction",
> ① . "title":"Moby Dick"
> }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
INFO->
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~
> {
> ① . "author":"J. R. R. Tolkien",
> ① . "price":22.99,
> ① . "isbn":"0-395-19395-8",
> ① . "category":"fiction",
> ① . "title":"The Lord of the Rings"
> }
~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~ JSON ~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~~☢~

Process finished with exit code 0

  • 公众号FunTester ,欢迎关注、交流,禁止第三方擅自转载。