「原创声明:保留所有权利,禁止转载」
书接上文: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首发,更多原创文章:FunTester440+ 原创文章,欢迎关注、交流,禁止第三方擅自转载。
热文精选
TesterHome 为用户提供「保留所有权利,禁止转载」的选项。
除非获得原作者的单独授权,任何第三方不得转载标注了「原创声明:保留所有权利,禁止转载」的内容,否则均视为侵权。
具体请参见TesterHome 知识产权保护协议。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。