JsonPath的使用
语法:
JsonPath |
描述 |
$ |
根节点 |
@ |
当前节点 |
.or[] |
子节点 |
.. |
选择所有符合条件的节点 |
* |
所有节点 |
[] |
迭代器标示,如数组下标 |
[,] |
支持迭代器中做多选 |
[start:end:step] |
数组切片运算符 |
?() |
支持过滤操作 |
() |
支持表达式计算 |
需要的jar包:
- commons-lang-2.6.jar
- json-path-0.8.1.jar
- json-smart-1.1.1.jar
对于如下的json,通过实例介绍JsonPath的使用
- { "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
- }
- }
- }
代码:
- private static void jsonPathTest() {
- JSONObject json = jsonTest();//调用自定义的jsonTest()方法获得json对象,生成上面的json
- //输出book[0]的author值
- String author = JsonPath.read(json, "$.store.book[0].author");
- //输出全部author的值,使用Iterator迭代
- List<String> authors = JsonPath.read(json, "$.store.book[*].author");
- //输出book[*]中category == 'reference'的book
- List<Object> books = JsonPath.read(json, "$.store.book[?(@.category == 'reference')]");
- //输出book[*]中price>10的book
- List<Object> books = JsonPath.read(json, "$.store.book[?(@.price>10)]");
- //输出book[*]中含有isbn元素的book
- List<Object> books = JsonPath.read(json, "$.store.book[?(@.isbn)]");
- //输出该json中所有price的值
- List<Double> prices = JsonPath.read(json, "$..price");
- //可以提前编辑一个路径,并多次使用它
- JsonPath path = JsonPath.compile("$.store.book[*]");
- List<Object> books = path.read(json);
- }
JsonPath的使用的更多相关文章
- restassured - JsonPath
https://github.com/rest-assured/rest-assured/blob/master/json-path/src/test/java/io/restassured/path ...
- jsonpath
1. java 类库 jayway/JsonPath maven 使用方法 <dependency> <groupId>com.jayway.jsonpath</grou ...
- 使用jsonpath解析json内容
JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...
- 好用的json-path
$.store.book[?(@.price < 10)].title Here is a complete overview and a side by side comparison of ...
- JsonPath详解
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...
- 一.HttpClient、JsonPath、JsonObject运用
HttpClient详细应用请参考官方api文档:http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.h ...
- jsonpath读取json数据格式公用方法!!!
import java.util.LinkedHashMap; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Pred ...
- Python爬虫(十六)_JSON模块与JsonPath
本篇将介绍使用,更多内容请参考:Python学习指南 数据提取之JSON与JsonPATH JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它是的人们很容易 ...
- JSONPath使用
JSONPath是fastjson在1.2.0之后支持的.JSONPath是一个很强大的功能.关于JSONPath的介绍请查看官方文档 JSONPath. 官方文档上给出了详细的说明以及使用.但是官方 ...
- Python_实现json数据的jsonPath(精简版)定位及增删改操作
基于python实现json数据的jsonPath(精简版)定位及增删改操作 by:授客 QQ:1033553122 实践环境 win7 64 Python 3.4.0 代码 #-*- encod ...
随机推荐
- 语音AT命令参考
不知道 这AT指令是不是通用的,尝试过的给我个回复 语音命令 命令 描述 +FCLASS=8 进入语音模式.AT+FCLASS=8 将调制解调器置入语音模式.扩音电话和TAM模式包括在通用语音模式中, ...
- Unity3D笔记 愤怒的小鸟<三> 实现Play界面2
前言:在Play页面中给Play页面添加一个“开始游戏”和“退出游戏”按钮顺便再来一个背景音乐 添加按钮可以是GUI.Button(),也可以是GUILayout.Button():给图片添加按钮可以 ...
- dubbo入门之微服务客户端服务端配置
正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的dubbo入门之helloWorld,我们再改改配置,即可实现正常的微服务架构.与之前相比 ...
- Android系统dimension单位详解
转载请注明出处,谢谢!http://www.cnblogs.com/coding-way/p/3457878.html Android设备种类多样,要想适配好各种屏幕,理解各种屏幕数据是必须的.首先先 ...
- vue--自定义指令进行验证(1)
实例代码: <template> <div id="app" class="app"> <h3>{{msg}}</h3 ...
- numpy中的reshape中参数为-1
上篇文章中的reshape(-1,2),有的时候不明白为什么会有参数-1,可以通过查找文档中的reshape()去理解这个问题 根据Numpy文档(https://docs.scipy.org/doc ...
- OSI互联数据包封装与解封装过程
当我们在七层协议最上层,主机A想和其它主机通信, 比如telnet到主机B,各层都为数据打包后再封装上自己能识别的数据标签,现在我们只说四层以下的通信过程. .当一个高层的数据包到达传输层,由于tel ...
- 关于jquery的css的一些知识
Query实例CSS 样式表动态选择本实例主要说的还是jquery的选择器,关于jquery的css的一些知识用类似 $("li").css("cursor", ...
- poj3259 Wormholes【最短路-bellman-负环】
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole ...
- oracle 日期格式化和数据去重
1.获取系统日期: select sysdate as date1 from dual: 当前时间减去7分钟的时间 select sysdate,sysdate - interval '7' MINU ...