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方便可靠的更多相关文章

  1. JMeter 插件 Json Path 解析 HTTP 响应 JSON 数据(转)

    JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为 JSON 数据格式.JSON 简介,JSON 教程. JSON 已经成为数据交换格式 ...

  2. JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串;JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象

    JSON.stringify()方法是将一个javascript值(对象或者数组)转换成为一个JSON字符串:JSON.parse()解析JSON字符串,构造由字符串描述的javascript值或对象

  3. Python | JSON 数据解析(Json & JsonPath)

    一.什么是JSON? JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范)的一 ...

  4. JMeter 插件 Json Path 解析HTTP响应JSON数据

    一.基本简介 JMeter 是一个不错的负载和性能测试工具,我们也用来做 HTTP API 接口测试.我们的 API 返回结果为JSON数据格式.JSON 简介,JSON 教程. JSON 已经成为数 ...

  5. SQL FOR JSON PATH 返回 json

    --直接返回 age FOR JSON PATH --返回值 [{"name":"张学友","age":60}] select c1, c2 ...

  6. Python3基础 json.loads 解析json格式的数据,得到一个字典

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. JSON.parse 解析json字符串时,遇换行符报错

    Json字符串转换成Json对象时候,有两种方式: 假设d是json字符串: 1,eval('(' + d + ')'). 2,JSON.parse(d): 但是以上方式有隐患,如果Json字符串有换 ...

  8. C#使用Json.NET解析Json

    本文转载自 http://xiaosheng.me/2016/10/01/article25/ 最近在 C# 项目中需要使用到 Json 格式的数据,我简单上网搜索了一下,基本上有两种操作 Json ...

  9. Android原生生成JSON与解析JSON

    JSON数据是一种轻量级的数据交换格式,在Android中通常应用于client与server交互之间的传输数据.像如今在网上有非常多解析JSON数据的jar包,可是归根究竟用的都是Android原生 ...

  10. scala解析json —— json4s 解析json方法汇总

    使用json4s的框架,包括spark,flink 1.org.json4s 引入pom的方法 对于本地支持,引入以下依赖项添加到pom中 <dependency> <groupId ...

随机推荐

  1. c++ 运算符重载operator

    一般格式为: 函数类型 operator 运算符名称(形参列表){ 对运算符的重载 } 注意函数名是由operator和运算符组成.在上面的一般格式中,operator是关键字,是专门用于重载运算符函 ...

  2. schema.xml属性概念

    # schema 定义逻辑库 checkSQLschema  当该值设置为 true 时,如果我们执行语句**select * from TESTDB.travelrecord;**则 MyCat 会 ...

  3. python正则 转

    python中的正则表达式(re模块)   一.简介 正则表达式本身是一种小型的.高度专业化的编程语言,而在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配.正则表达式模式被编 ...

  4. BZOJ3561 DZY Loves Math VI 【莫比乌斯反演】

    题目 给定正整数n,m.求 输入格式 一行两个整数n,m. 输出格式 一个整数,为答案模1000000007后的值. 输入样例 5 4 输出样例 424 提示 数据规模: 1<=n,m<= ...

  5. linux文件属性详解及文件类型

    一  drwxr-xr-x的意思解释: ls -al 得到如下列表: drwxr-xr-x oracle dba May : oralog1 drwxr-x--- root root May : ro ...

  6. 2017-2018-2 20179204《网络攻防实践》第十一周学习总结 SQL注入攻击与实践

    第1节 研究缓冲区溢出的原理,至少针对两种数据库进行差异化研究 1.1 原理 在计算机内部,输入数据通常被存放在一个临时空间内,这个临时存放的空间就被称为缓冲区,缓冲区的长度事先已经被程序或者操作系统 ...

  7. Html.AntiForgeryToken 防止伪造提交

    原文发布时间为:2011-05-03 -- 来源于本人的百度文章 [由搬家工具导入] In this tutorial, I am not going to discuss the concept i ...

  8. 【SQL Server】修改DB逻辑文件名称

    步骤一:查询当前DB逻辑文件名称(主逻辑文件.日志逻辑文件) ; 步骤二:步骤二改变(还原)DB逻辑文件名称 RESTORE DATABASE AW831 FROM DISK='D:\AW831.DA ...

  9. webRTC实战总结

    前言 前段时间一直在忙一个基于WebRTC的PC和移动端双向视频的项目.第一次接触webRTC,难免遇到了许多问题,比如:webRTC移动端兼容性检测,如何配置MediaStreamConstrain ...

  10. WSDL协议简单介绍

    WSDL – WebService Description Language – Web服务描述语言 通过XML形式说明服务在什么地方-地址. 通过XML形式说明服务提供什么样的方法 – 如何调用. ...