语法:

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的使用的更多相关文章

  1. restassured - JsonPath

    https://github.com/rest-assured/rest-assured/blob/master/json-path/src/test/java/io/restassured/path ...

  2. jsonpath

    1. java 类库 jayway/JsonPath maven 使用方法 <dependency> <groupId>com.jayway.jsonpath</grou ...

  3. 使用jsonpath解析json内容

    JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容.下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它. 一.首先需要 ...

  4. 好用的json-path

    $.store.book[?(@.price < 10)].title Here is a complete overview and a side by side comparison of ...

  5. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...

  6. 一.HttpClient、JsonPath、JsonObject运用

    HttpClient详细应用请参考官方api文档:http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.h ...

  7. jsonpath读取json数据格式公用方法!!!

    import java.util.LinkedHashMap; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.Pred ...

  8. Python爬虫(十六)_JSON模块与JsonPath

    本篇将介绍使用,更多内容请参考:Python学习指南 数据提取之JSON与JsonPATH JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它是的人们很容易 ...

  9. JSONPath使用

    JSONPath是fastjson在1.2.0之后支持的.JSONPath是一个很强大的功能.关于JSONPath的介绍请查看官方文档 JSONPath. 官方文档上给出了详细的说明以及使用.但是官方 ...

  10. Python_实现json数据的jsonPath(精简版)定位及增删改操作

    基于python实现json数据的jsonPath(精简版)定位及增删改操作   by:授客 QQ:1033553122 实践环境 win7 64 Python 3.4.0 代码 #-*- encod ...

随机推荐

  1. Particle 粒子效果使用(适配微信小游戏,particle is not defined)

    在微信小游戏中使用粒子效果 参考: 1. 粒子库下载地址 2. 粒子官方使用教程 3. 水友解决微信小游戏particle is not defined 一.下载第三方库 Git地址:https:// ...

  2. Docker logs 命令

    [root@docker01 ~]# docker logs --help Usage: docker logs [OPTIONS] CONTAINER Fetch the logs of a con ...

  3. 9.11 Django视图 view和路由

    2018-9-11 16:34:16 2018-9-11 19:00:24 越努力,.越幸运! Django框架参考: https://www.cnblogs.com/liwenzhou/p/8296 ...

  4. Asp.net MVC]Asp.net MVC5系列——Routing特性

    目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...

  5. java.sql.SQLException:The Network Adapter could not establish the connection

    数据库连不上了,可能数据断了或者修改IP了

  6. ububtu16.04下安装protobuf

    重新下载protobuf,我下载的时最新的protobuf-all-3.5.1.tar.gz   protobuf网址:https://github.com/google/protobuf/relea ...

  7. 新建的web工程找不到javax.servlet.http.httpservlet

    1.在出现此错误的项目上右键-->Build Path --> Configure Build Path 2.点击右边的 ADD Library 3.选中“Server Runtime”, ...

  8. Hibernate错误

    1.Field 'id' doesn't have a default value 原来是我的数据设计的时候,把主键的类型定义为int的,原本想是用自增的方式来的,可是由于自己的粗心,写sql语句的时 ...

  9. 虚拟机中实现Linux与Windows之间的文件传输

    虚拟机中实现Linux与Windows之间的文件传输 标签: linux 2016年06月28日 11:17:37 2092人阅读 评论(0) 收藏 举报  分类: linux(2)    一.配置环 ...

  10. python float()

    1.函数功能将一个数值或者字符转换成浮点型数值 >>> a = 12345 >>> amount = float(a) >>> print(amo ...