JsonPath入门教程
有时候需要从json里面提取相关数据,必须得用到如何提取信息的知识,下面来写一下
语法格式
JsonPath |
描述 |
$ |
根节点 |
@ |
当前节点 |
.or[] |
子节点 |
.. |
选择所有符合条件的节点 |
* |
所有节点 |
[] |
迭代器标示,如数组下标 |
[,] |
支持迭代器中做多选 |
[start:end:step] |
数组切片运算符 |
?() |
支持过滤操作 |
() |
支持表达式计算 |
json格式的数据:
{ "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
}
}
}
测试代码:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath; import java.util.List; /*
{ "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
}
}
}
*/ public class JsonPathTest {
private static String jsonStr="{ \"store\": {\n" +
" \"book\": [ \n" +
" { \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" },\n" +
" { \"category\": \"fiction\",\n" +
" \"author\": \"Evelyn Waugh\",\n" +
" \"title\": \"Sword of Honour\",\n" +
" \"price\": 12.99,\n" +
" \"isbn\": \"0-553-21311-3\"\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" }\n" +
"}"; public static void main(String[] args) {
JSONObject json = JSON.parseObject(jsonStr);//调用自定义的jsonTest()方法获得json对象,生成上面的json //输出book[0]的author值
String author = JsonPath.read(json, "$.store.book[0].author");
System.out.println(author);
List<String> authorList = JsonPath.read(json, "$.store.book[*].author");
for(String authors:authorList){
System.out.println(authors);
} }
}
测试结果
C:\JAVAS\jdk\bin\java "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=19319:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF- -classpath C:\JAVAS\jdk\jre\lib\charsets.jar;C:\JAVAS\jdk\jre\lib\deploy.jar;C:\JAVAS\jdk\jre\lib\ext\access-bridge-.jar;C:\JAVAS\jdk\jre\lib\ext\cldrdata.jar;C:\JAVAS\jdk\jre\lib\ext\dnsns.jar;C:\JAVAS\jdk\jre\lib\ext\jaccess.jar;C:\JAVAS\jdk\jre\lib\ext\jfxrt.jar;C:\JAVAS\jdk\jre\lib\ext\localedata.jar;C:\JAVAS\jdk\jre\lib\ext\nashorn.jar;C:\JAVAS\jdk\jre\lib\ext\sunec.jar;C:\JAVAS\jdk\jre\lib\ext\sunjce_provider.jar;C:\JAVAS\jdk\jre\lib\ext\sunmscapi.jar;C:\JAVAS\jdk\jre\lib\ext\sunpkcs11.jar;C:\JAVAS\jdk\jre\lib\ext\zipfs.jar;C:\JAVAS\jdk\jre\lib\javaws.jar;C:\JAVAS\jdk\jre\lib\jce.jar;C:\JAVAS\jdk\jre\lib\jfr.jar;C:\JAVAS\jdk\jre\lib\jfxswt.jar;C:\JAVAS\jdk\jre\lib\jsse.jar;C:\JAVAS\jdk\jre\lib\management-agent.jar;C:\JAVAS\jdk\jre\lib\plugin.jar;C:\JAVAS\jdk\jre\lib\resources.jar;C:\JAVAS\jdk\jre\lib\rt.jar;C:\Users\admin\diamond4\java-lesson2\lesson2-start\target\classes;D:\mvn_repository\org\springframework\boot\spring-boot-starter-web\1.5..RELEASE\spring-boot-starter-web-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-tomcat\1.5..RELEASE\spring-boot-starter-tomcat-1.5..RELEASE.jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.\tomcat-embed-core-8.5..jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.\tomcat-embed-el-8.5..jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.\tomcat-embed-websocket-8.5..jar;D:\mvn_repository\org\hibernate\hibernate-validator\5.3..Final\hibernate-validator-5.3..Final.jar;D:\mvn_repository\javax\validation\validation-api\1.1..Final\validation-api-1.1..Final.jar;D:\mvn_repository\org\jboss\logging\jboss-logging\3.3..Final\jboss-logging-3.3..Final.jar;D:\mvn_repository\com\fasterxml\classmate\1.3.\classmate-1.3..jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-databind\2.8.\jackson-databind-2.8..jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-annotations\2.8.\jackson-annotations-2.8..jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-core\2.8.\jackson-core-2.8..jar;D:\mvn_repository\org\springframework\spring-web\4.3..RELEASE\spring-web-4.3..RELEASE.jar;D:\mvn_repository\org\springframework\spring-webmvc\4.3..RELEASE\spring-webmvc-4.3..RELEASE.jar;D:\mvn_repository\org\springframework\spring-expression\4.3..RELEASE\spring-expression-4.3..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-aop\1.5..RELEASE\spring-boot-starter-aop-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\spring-aop\4.3..RELEASE\spring-aop-4.3..RELEASE.jar;D:\mvn_repository\org\aspectj\aspectjweaver\1.8.\aspectjweaver-1.8..jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-mail\1.5..RELEASE\spring-boot-starter-mail-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\spring-context\4.3..RELEASE\spring-context-4.3..RELEASE.jar;D:\mvn_repository\com\sun\mail\javax.mail\1.5.\javax.mail-1.5..jar;D:\mvn_repository\javax\activation\activation\1.1\activation-1.1.jar;D:\mvn_repository\org\springframework\spring-test\4.3..RELEASE\spring-test-4.3..RELEASE.jar;D:\mvn_repository\jfree\jfreechart\1.0.\jfreechart-1.0..jar;D:\mvn_repository\jfree\jcommon\1.0.\jcommon-1.0..jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-starter\0.1.\dubbo-spring-boot-starter-0.1..jar;D:\mvn_repository\com\alibaba\dubbo\2.5.\dubbo-2.5..jar;D:\mvn_repository\org\javassist\javassist\3.21.-GA\javassist-3.21.-GA.jar;D:\mvn_repository\org\jboss\netty\netty\3.2..Final\netty-3.2..Final.jar;D:\mvn_repository\org\apache\zookeeper\zookeeper\3.4.\zookeeper-3.4..jar;D:\mvn_repository\log4j\log4j\1.2.\log4j-1.2..jar;D:\mvn_repository\jline\jline\0.9.\jline-0.9..jar;D:\mvn_repository\io\netty\netty\3.10..Final\netty-3.10..Final.jar;D:\mvn_repository\org\apache\curator\curator-framework\2.12.\curator-framework-2.12..jar;D:\mvn_repository\org\apache\curator\curator-client\2.12.\curator-client-2.12..jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-autoconfigure\0.1.\dubbo-spring-boot-autoconfigure-0.1..jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-actuator\0.1.\dubbo-spring-boot-actuator-0.1..jar;D:\mvn_repository\mysql\mysql-connector-java\5.1.\mysql-connector-java-5.1..jar;D:\mvn_repository\com\google\guava\guava\22.0\guava-22.0.jar;D:\mvn_repository\com\google\code\findbugs\jsr305\1.3.\jsr305-1.3..jar;D:\mvn_repository\com\google\errorprone\error_prone_annotations\2.0.\error_prone_annotations-2.0..jar;D:\mvn_repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;D:\mvn_repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;D:\mvn_repository\org\apache\commons\commons-lang3\3.5\commons-lang3-3.5.jar;D:\mvn_repository\org\apache\httpcomponents\httpclient\4.5.\httpclient-4.5..jar;D:\mvn_repository\org\apache\httpcomponents\httpcore\4.4.\httpcore-4.4..jar;D:\mvn_repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\mvn_repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;D:\mvn_repository\commons-io\commons-io\2.5\commons-io-2.5.jar;D:\mvn_repository\com\alibaba\fastjson\1.2.\fastjson-1.2..jar;D:\mvn_repository\org\quartz-scheduler\quartz\2.2.\quartz-2.2..jar;D:\mvn_repository\c3p0\c3p0\0.9.1.1\c3p0-0.9.1.1.jar;D:\mvn_repository\org\slf4j\slf4j-api\1.7.\slf4j-api-1.7..jar;D:\mvn_repository\org\quartz-scheduler\quartz-jobs\2.2.\quartz-jobs-2.2..jar;D:\mvn_repository\com\jayway\jsonpath\json-path\2.2.\json-path-2.2..jar;D:\mvn_repository\net\minidev\json-smart\2.2.\json-smart-2.2..jar;D:\mvn_repository\net\minidev\accessors-smart\1.1\accessors-smart-1.1.jar;D:\mvn_repository\org\ow2\asm\asm\5.0.\asm-5.0..jar;D:\mvn_repository\org\codehaus\groovy\groovy-all\2.1.\groovy-all-2.1..jar;D:\mvn_repository\org\projectlombok\lombok-maven-plugin\1.16.18.1\lombok-maven-plugin-1.16.18.1.jar;D:\mvn_repository\org\projectlombok\lombok\1.16.\lombok-1.16..jar;D:\mvn_repository\org\sonatype\plexus\plexus-build-api\0.0.\plexus-build-api-0.0..jar;D:\mvn_repository\org\codehaus\plexus\plexus-utils\1.5.\plexus-utils-1.5..jar;D:\mvn_repository\org\springframework\boot\spring-boot-configuration-processor\1.5..RELEASE\spring-boot-configuration-processor-1.5..RELEASE.jar;D:\mvn_repository\com\vaadin\external\google\android-json\0.0..vaadin1\android-json-0.0..vaadin1.jar;D:\mvn_repository\org\jsoup\jsoup\1.9.\jsoup-1.9..jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-freemarker\1.5..RELEASE\spring-boot-starter-freemarker-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter\1.5..RELEASE\spring-boot-starter-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-logging\1.5..RELEASE\spring-boot-starter-logging-1.5..RELEASE.jar;D:\mvn_repository\ch\qos\logback\logback-classic\1.1.\logback-classic-1.1..jar;D:\mvn_repository\ch\qos\logback\logback-core\1.1.\logback-core-1.1..jar;D:\mvn_repository\org\slf4j\jcl-over-slf4j\1.7.\jcl-over-slf4j-1.7..jar;D:\mvn_repository\org\slf4j\jul-to-slf4j\1.7.\jul-to-slf4j-1.7..jar;D:\mvn_repository\org\slf4j\log4j-over-slf4j\1.7.\log4j-over-slf4j-1.7..jar;D:\mvn_repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;D:\mvn_repository\org\freemarker\freemarker\2.3.-incubating\freemarker-2.3.-incubating.jar;D:\mvn_repository\org\springframework\spring-context-support\4.3..RELEASE\spring-context-support-4.3..RELEASE.jar;D:\mvn_repository\org\springframework\spring-beans\4.3..RELEASE\spring-beans-4.3..RELEASE.jar;D:\mvn_repository\org\apache\poi\poi-ooxml\3.17\poi-ooxml-3.17.jar;D:\mvn_repository\org\apache\poi\poi\3.17\poi-3.17.jar;D:\mvn_repository\org\apache\commons\commons-collections4\4.1\commons-collections4-4.1.jar;D:\mvn_repository\org\apache\poi\poi-ooxml-schemas\3.17\poi-ooxml-schemas-3.17.jar;D:\mvn_repository\org\apache\xmlbeans\xmlbeans\2.6.\xmlbeans-2.6..jar;D:\mvn_repository\stax\stax-api\1.0.\stax-api-1.0..jar;D:\mvn_repository\com\github\virtuald\curvesapi\1.04\curvesapi-1.04.jar;D:\mvn_repository\com\squareup\okhttp3\okhttp\3.2.\okhttp-3.2..jar;D:\mvn_repository\com\squareup\okio\okio\1.6.\okio-1.6..jar;D:\mvn_repository\org\springframework\spring-core\4.3..RELEASE\spring-core-4.3..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-devtools\1.5..RELEASE\spring-boot-devtools-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot\1.5..RELEASE\spring-boot-1.5..RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-autoconfigure\1.5..RELEASE\spring-boot-autoconfigure-1.5..RELEASE.jar com.longteng.lesson2.service.JsonPathTest
::59.081 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][]['author']
Nigel Rees
::59.096 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][*]['author']
Nigel Rees
Evelyn Waugh Process finished with exit code
JsonPath入门教程的更多相关文章
- MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB
Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...
- wepack+sass+vue 入门教程(三)
十一.安装sass文件转换为css需要的相关依赖包 npm install --save-dev sass-loader style-loader css-loader loader的作用是辅助web ...
- wepack+sass+vue 入门教程(二)
六.新建webpack配置文件 webpack.config.js 文件整体框架内容如下,后续会详细说明每个配置项的配置 webpack.config.js直接放在项目demo目录下 module.e ...
- wepack+sass+vue 入门教程(一)
一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...
- Content Security Policy 入门教程
阮一峰文章:Content Security Policy 入门教程
- gulp详细入门教程
本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...
- UE4新手引导入门教程
请大家去这个地址下载:file:///D:/UE4%20Doc/虚幻4新手引导入门教程.pdf
- ABP(现代ASP.NET样板开发框架)系列之2、ABP入门教程
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之2.ABP入门教程 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...
- webpack入门教程之初识loader(二)
上一节我们学习了webpack的安装和编译,这一节我们来一起学习webpack的加载器和配置文件. 要想让网页看起来绚丽多彩,那么css就是必不可少的一份子.如果想要在应用中增加一个css文件,那么w ...
随机推荐
- 使用labelImg制作自己的数据集(VOC2007格式)用于Faster-RCNN训练
https://blog.csdn.net/u011956147/article/details/53239325 https://blog.csdn.net/u011574296/article/d ...
- shell脚本判断进程是否运行
zzx@zzx120:~$ if ps aux | grep "python"|grep -v grep > /dev/null #$?的值不同 th ...
- 计算机ASCII码对照表
ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ! 65 A 97 a 2 ST ...
- 调用支付宝接口的简单demo
依赖: <!-- alipay-sdk-java 注意一下版本--> <dependency> <groupId>com.alipay.sdk</groupI ...
- CSS3新属性:在网站中使用访客电脑里没有安装的字体
CSS的font-family属性使网页可以使用客户电脑里的字体,从而得到多姿多彩的WEB页面,但当客户端没有你想要使用的字体时怎么办呢?我们总不能让每个访问者都去安装一个字体吧?事实上,这是可以的! ...
- POP3、SMTP和IMAP基础概念
POP3 POP3是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议.它是因特网电子邮件的第 ...
- sybase连接失败 JZ006: Caught IOException: java.net.ConnectException处理方式
windows系统下的处理办法: 1.查找端口为5000的进程的pid: 在cmd窗口中输入 netstat -ano 我这里是2324. 打开任务管理器,找到pid是2324的进程,结束进程. 打开 ...
- scrapy补充-分布式爬虫
spiders 介绍:在项目中是创建爬虫程序的py文件 #1.Spiders是由一系列类(定义了一个网址或一组网址将被爬取)组成,具体包括如何执行爬取任务并且如何从页面中提取结构化的数据. #2.换句 ...
- 非线性支持向量机SVM
非线性支持向量机SVM 对于线性不可分的数据集, 我们引入了核(参考:核方法·核技巧·核函数) 线性支持向量机的算法如下: 将线性支持向量机转换成非线性支持向量机只需要将变为核函数即可: 非线性支持向 ...
- 几个Java基础题
1.java中线程能不能重复start t1.start(); System.out.println("ssss"); t1.start(); 答:第一 ...