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. idea中将项目与github关联

    © 版权声明:本文为博主原创文章,转载请注明出处 1.在github中创建一个账号:https://github.com/join?source=header-home 2.下载并安装git:http ...

  2. ValueStack、ActionContext

    笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 1. ValueStac ...

  3. PAT1032

    为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...

  4. Linux内存使用消耗高

    Linux系统下如果内存占用很高又找不到是被什么程序占用的,需要考虑下是否是SLAB的问题.SLAB是Linux操作系统的一种内存分配机制,可以使用下面命令来查看.例如: cat /proc/memi ...

  5. 【bzoj2179】FFT快速傅立叶 FFT

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出 输出一行,即x*y的结果. 样例 ...

  6. [kuangbin带你飞]专题十一 网络流个人题解(L题留坑)

    A - ACM Computer Factory 题目描述:某个工厂可以利用P个部件做一台电脑,有N个加工用的机器,但是每一个机器需要特定的部分才能加工,给你P与N,然后是N行描述机器的最大同时加工数 ...

  7. ext2 与 ext3

    http://linux.vbird.org/linux_basic/1010appendix_B.php https://baike.baidu.com/item/Ext2/822106?fr=al ...

  8. 平滑升级nginx

    平滑升级nginx版本技术文档 作者 联系方式 日期 版本号 马坤 852115346@qq.com 2017-12-31 V1.0.0 备注:作者水平有限,难免出现错误.如若发现错误,请您及时与作者 ...

  9. 【简单的原创】div简单轮换显示

    原文发布时间为:2009-05-10 -- 来源于本人的百度文章 [由搬家工具导入] <html><head><title>div简单轮换显示</title& ...

  10. 無法使用 adb push file,Read-only file system

    adb root adb remount adb push xxx /system/etc/xxx failed to copy 'xxx' to '/system/etc/xxx': couldn' ...