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. 微信小程序--问题汇总及详解之picker 增、删

    <block wx:for="{{salesList}}" wx:for-index="index" wx:key="id" wx:f ...

  2. Spring 简单而强大的事务管理功能

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  3. POJ2796 Feel Good -- 单调队列

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 4015 Case T ...

  4. selenium webdriver——元素操作

    #Author:xiaoxiao from selenium import webdriver import time def abcd(): driver = webdriver.Firefox() ...

  5. [openjudge6043]哆啦A梦的时光机

    [openjudge6043]哆啦A梦的时光机 试题描述 哆啦A梦有一个神奇的道具:时光机.坐着它,大雄和他的伙伴们能穿越时空,回到过去或者去到未来. 有一天,大雄和他的伙伴们想穿越时空进行探险,可是 ...

  6. [NOI2010][bzoj2006] 超级钢琴 [主席树/ST表+堆]

    题面: 传送门 思路: 首先容易想到用堆维护的O(n2logn)暴力 那么肯定就是在这个基础上套数据结构了[愉快] 然而我因为过于蒟蒻......只想得到主席树暴力***过去的方法 大概就是把前缀和算 ...

  7. mac python 安装参考

    首先需明确: Mac 电脑上自带有 python 查看默认的 python 版本,打开终端输入命令 python,即可看到如下内容: 我的系统版本OS X 10.13.2,自带的Python版本是2. ...

  8. 【04】【转】Nodejs学习笔记(四)--- 与MySQL交互(felixge/node-mysql)

    目录 简介和安装 测试MySQL 认识一下Connection Options MYSQL CURD 插入 更新 查询 删除 Nodejs 调用带out参数的存储过程,并得到out参数返回值 结束数据 ...

  9. AssetDatabase.RenameAsset 重命名文件失败

    今天想写一段Unity Editor 的代码将在 Project Panel 中选中的所有 Texture 改变 Format,然后重命名 成 xxx.Dither.png 然后自动进行上一篇文章提到 ...

  10. 事务的四大属性ACID即事务的原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)、持久性(Durability.。

    事务的四大属性ACID即事务的原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability.. 原子性(Atomicity) 原子性是指事 ...