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 ...
随机推荐
- Go-方法-接口-异常处理-错误处理
方法 什么是方法? 方法其实就是一个函数,在 func 这个关键字和方法名中间加入了一个特殊的接收器类型.接收器可以是结构体类型或者是非结构体类型.接收器是可以在方法的内部访问的. package m ...
- caffe-fasterrcnn程序理解
faster-rcnn 结构杂谈 参考博客:::https://www.cnblogs.com/longriyao/p/5832274.html http://www.cnblogs.com/cha ...
- Spring DATA Neo4J(一)
Spring DATA Neo4J——简介 Spring Framework提供了一下模块来处理基于Java的应用程序的DAO层 Spring JDBC Spring ORM Spring DATA ...
- Hadoop的常用指令
-help:查看帮助 hadoop fs -help rm -rm [-f] [-r|-R] [-skipTrash] <src> ... : Delete all files that ...
- 2.react 基础 - create-react-app 目录结构 及 组件应用
1. react-app 脚手架的 目录结构 node_modules -d 存放 第三方下载的 依赖的包 public -d 资源目录 favicon.ico - 左上角的图标 index.h ...
- winter_holiday_2019
个人逐利对他人的影响:私营企业主的个人付出.作为长辈对晚辈的付出 是2020要实现精准扶贫吗?精准扶贫的概念,是针对一个家庭还是针对家里的每个人的平均收入.精准扶贫比教育更有作用,刘传铁(湖北省委高校 ...
- 在项目中ES6语法转ES5语法 babel转码器
es6 babel 安装以及使用 1,安装好node(需要使用npm包管理工具) 2,在本地项目路径下npm init,格式化成功后会在项目下生成一个配置文件package.json 3,本地安装 ...
- Linux-使用syslog来记录调试信息
1.有三个函数:openlog.syslog.closelog 2.一般的log信息都在操作系统的/var/log/messages这个文件中存储着,但是ubuntu中是在/var/log/syslo ...
- leetcode 746. 使用最小花费爬楼梯
题目: 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost[i](索引从0开始). 每当你爬上一个阶梯你都要花费对应的体力花费值,然后你可以选择继续爬一个阶梯或者爬两个阶梯 ...
- 基于Flask框架搭建视频网站的学习日志(六)之数据库
使用Flask-SQLSlchemy管理数据库(1)--初步安装调试 一.介绍: Flask-SQLSlchemy是一个Flask扩展,简化了Flask中对sql的操作,是一个高层的框架,可以避免直接 ...