JsonPath (JSON 解析神器)
来源:互联网 发布:mac上绘画软件 编辑:程序博客网 时间:2024/05/15 16:41
再复杂的 Json 都能给你解析出来,非常方便的获取 JSON 的内容,很强大!
语法简介
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, "isbn": "0-553-21311-3" } ], "bicycle": { "color": "red", "price": 19.95 } }}
导包: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);
用法比较简单,多使用几次就会使用了!文章主要参考网上!
阅读全文
0 0
- JsonPath (JSON 解析神器)
- json解析神器 jsonpath的使用
- JsonPath —— JSON 解析神器
- 使用jsonpath解析json内容
- 使用JsonPath解析json数据
- jsonpath解析json数据的javascript版本
- python爬虫 如何解析json文件 json文件的解析提取和jsonpath的应用
- JsonPath
- jsonpath
- 数据提取之JSON与JsonPATH
- Python爬虫json和JsonPath系列之十一
- Python中Json对象处理的jsonpath-rw
- 比Gson解析速度快10倍!——Json解析神器Jackson使用介绍
- 比Gson解析速度快10倍!——Json解析神器Jackson使用介绍
- 比Gson解析速度快10倍!——Json解析神器Jackson使用介绍
- jsoup解析html神器
- 神器ViewDragHelper完全解析
- JsonPath教程
- 使用webpack打包react项目中遇到的问题(一)
- Python 多线程搜索txt中的内容
- Android欢迎页面闪屏解决方法
- Docker容器技术介绍(三) --- Docker容器
- 安装、登陆CentOS7
- JsonPath (JSON 解析神器)
- Spring Boot 整合 Thymeleaf 完整 Web 案例
- java爬虫绕过登录页面
- Xcode 中放置图片的位置,Images.xcassets 和直接放在源文件目录中的区别
- Pycharm清除波浪线
- 关于response.getWriter().write()返回int类型给前台的乱码问题
- 容器加載Web工程的Web.xml文件介紹
- ZooKeepr日志清理
- Tarjan求LCA