温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JSON 解析神器JsonPath怎么用

发布时间:2021-12-13 19:05:34 来源:亿速云 阅读:374 作者:柒染 栏目:大数据

JSON 解析神器JsonPath怎么用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。


语法简介

JsonPath描述
$根节点
@当前节点
.or[]子节点
..选择所有符合条件的节点
*所有节点
[]迭代器标示,如数组下标
[,]支持迭代器中做多选
[start:end:step]数组切片运算符
?()支持过滤操作
()支持表达式计算

JSON 值:

JSON 解析神器JsonPath怎么用

导包:import com.jayway.jsonpath.JsonPath

解析代码:

//输出book[0]的author值String author = JsonPath.read(json, "$.store.book[0].author");System.out.println("author\t"+author);
//输出全部author的值,使用Iterator迭代List
<String> authors = JsonPath.read(json, "$.store.book[*].author");System.out.println("authors\t"+authors);
//输出book[*]中category == 'reference'的bookList<Object> books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");
System.out.println("books\t"+books);
//输出book[*]中category == 'reference'的book或者List<Object> books2 = JsonPath.read(json, "$.store.book[?(@.category == 'reference' || @.price>10)]");
System.out.println("books2\t"+books2);
//输出book[*]中category == 'reference'的book的authorList<Object> books1 = JsonPath.read(json, "$.store.book[?(@.category == 'reference')].author");
System.out.println("books1\t"+books1);
//输出book[*]中price>10的bookList<Object> b1 = JsonPath.read(json, "$.store.book[?(@.price>10)]");
System.out.println("b1"+b1);
//输出book[*]中含有isbn元素的bookList<Object> b2 = JsonPath.read(json, "$.store.book[?(@.isbn)]");
System.out.println("b2"+b2);
//输出该json中所有price的值List<Double> prices = JsonPath.read(json, "$..price");
System.out.println("prices"+prices);
//输出该json中所有title的值List<Double> title = JsonPath.read(json, "$..title");
System.out.println("title"+title);
//输出该json中book 0,1的值List<Double> book01 = JsonPath.read(json, "$..book[0,1]");
System.out.println("book01"+book01);
/* //输出该json中book 0,1的值List<Double> book012 = JsonPath.read(json, "$..book[-2:]");
System.out.println("book012"+book012);
*///可以提前编辑一个路径,并多次使用它JsonPath path = JsonPath.compile("$.store.book[*]");
List<Object> b3 = path.read(json);System.out.println("path\t"+path+"\n"+b3);

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI