json-path解析json方便可靠
JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPath is available in many programming languages such as Javascript, Python and PHP. Now also in Java!
News
2013
-
09
-
27
Released
0.9
.
0
bug fixes, general improvements
2012
-
04
-
16
Released
0.8
.
1
bug fixes, improved docs, general improvements
2012
-
03
-
08
Released
0.8
.
0
bug fixes, Filter builder, Json model, POJO mapping (optional) and compliance improvements.
2012
-
02
-
09
Released
0.5
.
6
including bug fixes and performance improvements.
Given
{
"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
}
}
}
Read
All authors:
List<String> authors = JsonPath.read(json,
"$.store.book[*].author"
);
Author of first book in store:
String author = JsonPath.read(json,
"$.store.book[1].author"
);
All books with category =
"reference"
List<Object> books = JsonPath.read(json,
"$.store.book[?(@.category == 'reference')]"
);
List<Object> books = JsonPath.read(json,
"$.store.book[?]"
, filter(where(
"category"
).is(
"reference"
)));
All books that cost more than
10
USD
List<Object> books = JsonPath.read(json,
"$.store.book[?(@.price > 10)]"
);
List<Object> books = JsonPath.read(json,
"$.store.book[?]"
, filter(where(
"price"
).gt(
10
)));
All books that have isbn
List<Object> books = JsonPath.read(json,
"$.store.book[?(@.isbn)]"
);
List<Object> books = JsonPath.read(json,
"$.store.book[?]"
, filter(where(
"isbn"
).exists(
true
)));
Chained filters
Filter filter = Filter.filter(Criteria.where(
"isbn"
).exists(
true
).and(
"category"
).in(
"fiction"
,
"reference"
))
List<Object> books = JsonPath.read(json,
"$.store.book[?]"
, filter);
Custom filters
Filter myFilter =
new
Filter.FilterAdapter<Map<String, Object>>(){
@Override
public
boolean
accept(Map<String, Object> map) {
return
map.containsKey(
"isbn"
);
}
};
List<Object> books = JsonPath.read(json,
"$.store.book[?]"
, myFilter);
All prices in the document
List<Double> prices = JsonPath.read(json,
"$..price"
);
Compiled path
You can pre compile a path and use it multiple times
JsonPath path = JsonPath.compile(
"$.store.book[*]"
);
List<Object> books = path.read(json);
Assert
Asserts are made with Hamcrest matchers
JsonAssert.with(json).assertThat(
"$.store.bicycle.color"
, Matchers.equalTo(
"red"
))
.assertThat(
"$.store.bicycle.price"
, Matchers.equalTo(
19
.95D));
Add some
static
imports and you get
this
with(json).assertThat(
"$.store.bicycle.color"
, equalTo(
"red"
))
.assertThat(
"$.store.bicycle.price"
, equalTo(
19
.95D));
The Hamcrest library contains a lot of different matchers and they can often be nested.
with(json).assertThat(
"$..author"
, hasItems(
"Nigel Rees"
,
"Evelyn Waugh"
))
.assertThat(
"$..author"
, is(collectionWithSize(equalTo(
2
))));
with(json).assertThat(
"$.store.book[?(@.category == 'x')]"
, emptyCollection());
If you don't find the matcher you need, roll your own.
Download
Json-path is available at Maven Central
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>
0.9
.
1
</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-
assert
</artifactId>
<version>
0.9
.
1
</version>
<scope>test</scope>
</dependency>
json-path解析json方便可靠的更多相关文章
- JMeter 插件 Json Path 解析 HTTP 响应 JSON 数据(转)
JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为 JSON 数据格式.JSON 简介,JSON 教程. JSON 已经成为数据交换格式 ...
- JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象
- Python | JSON 数据解析(Json & JsonPath)
一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...
- JMeter 插件 Json Path 解析HTTP响应JSON数据
一.基本简介 JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为JSON数据格式.JSON 简介,JSON 教程. JSON 已经成为数 ...
- SQL FOR JSON PATH 返回 json
--直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...
- Python3基础 json.loads 解析json格式的数据,得到一个字典
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- JSON.parse 解析json字符串时,遇换行符报错
Json字符串转换成Json对象时候,有两种方式: 假设d是json字符串: 1,eval('(' + d + ')'). 2,JSON.parse(d): 但是以上方式有隐患,如果Json字符串有换 ...
- C#使用Json.NET解析Json
本文转载自 http://xiaosheng.me/2016/10/01/article25/ 最近在 C# 项目中需要使用到 Json 格式的数据,我简单上网搜索了一下,基本上有两种操作 Json ...
- Android原生生成JSON与解析JSON
JSON数据是一种轻量级的数据交换格式,在Android中通常应用于client与server交互之间的传输数据.像如今在网上有非常多解析JSON数据的jar包,可是归根究竟用的都是Android原生 ...
- scala解析json —— json4s 解析json方法汇总
使用json4s的框架,包括spark,flink 1.org.json4s 引入pom的方法 对于本地支持,引入以下依赖项添加到pom中 <dependency> <groupId ...
随机推荐
- csapp读书笔记-并发编程
这是基础,理解不能有偏差 如果线程/进程的逻辑控制流在时间上重叠,那么就是并发的.我们可以将并发看成是一种os内核用来运行多个应用程序的实例,但是并发不仅在内核,在应用程序中的角色也很重要. 在应用级 ...
- 【bzoj5018】[Snoi2017]英雄联盟 背包dp
题目描述 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄,因此,他也只准 ...
- [bzoj4259][bzoj4503] 残缺的字符串 [FFT]
题面 传送门 bzoj上的这两题是一样的...... 正文 我看到这道题,第一想法是跑魔改过的KMP,然后很快发现不可行 于是想换个角度思考 其实,本题最大的问题就在于通配符的存在:它可以匹配任意一个 ...
- fetch上传cookie数据方法
Fetch 请求默认是不带cookie的.需要设置fetch的第二个参数: 先来看下,请求头部信息Request method - 使用的HTTP动词,GET, POST, PUT, DELETE, ...
- 使用PropTypes进行类型检测
PropTypes 是react提供的用于检验数据类型的typechecking,避免应用越来越大的时候出现意料之外的bug class Greeting extends React.Componen ...
- [vuex] vuex requires a Promise polyfill in this browser报错问题的解决办法
在IE下由于不支持promise而导致的问题,需要插件babel-prolyfill cnpm i bablel-prolyfill -D 接着在webpack.config.js当中进行配置 ent ...
- create-react-app 配置支持sass并集成autoprefixer插件
create-react-app的webpack配置在node_modules当中的react-scripts的config文件夹当中,其中webpack.config.dev.js是开发环境的配置, ...
- Adreno Profiler分析任意安卓游戏特效+抓取资源
听说可以抓去任意游戏特效..保存下,有空研究 AdrenoProfiler 下载地址 Adreno Profiler分析任意安卓游戏特效+抓取资源 教程
- java--Eclipse for mac 代码提示(代码助手,代码联想)快捷键修改
Eclipse for mac 代码提示(代码助手,代码联想)快捷键修改 一.每次输入都自动提示 点击Eclipse,使其成为第一响应者,preferences->Java->Editor ...
- 【CF1043A】Elections(签到)
题意:给定n个数字,第i个为a[i],求使得sigma k-a[i]>sigma a[i]最小的k n,a[i]<=1e2 思路: #include<cstdio> #incl ...