有时候需要从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 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-8 -classpath C:\JAVAS\jdk\jre\lib\charsets.jar;C:\JAVAS\jdk\jre\lib\deploy.jar;C:\JAVAS\jdk\jre\lib\ext\access-bridge-32.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.6.RELEASE\spring-boot-starter-web-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.6.RELEASE\spring-boot-starter-tomcat-1.5.6.RELEASE.jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.16\tomcat-embed-core-8.5.16.jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.16\tomcat-embed-el-8.5.16.jar;D:\mvn_repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.16\tomcat-embed-websocket-8.5.16.jar;D:\mvn_repository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;D:\mvn_repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\mvn_repository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;D:\mvn_repository\com\fasterxml\classmate\1.3.3\classmate-1.3.3.jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-databind\2.8.9\jackson-databind-2.8.9.jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;D:\mvn_repository\com\fasterxml\jackson\core\jackson-core\2.8.9\jackson-core-2.8.9.jar;D:\mvn_repository\org\springframework\spring-web\4.3.10.RELEASE\spring-web-4.3.10.RELEASE.jar;D:\mvn_repository\org\springframework\spring-webmvc\4.3.10.RELEASE\spring-webmvc-4.3.10.RELEASE.jar;D:\mvn_repository\org\springframework\spring-expression\4.3.10.RELEASE\spring-expression-4.3.10.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-aop\1.5.6.RELEASE\spring-boot-starter-aop-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\spring-aop\4.3.10.RELEASE\spring-aop-4.3.10.RELEASE.jar;D:\mvn_repository\org\aspectj\aspectjweaver\1.8.10\aspectjweaver-1.8.10.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-mail\1.5.6.RELEASE\spring-boot-starter-mail-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\spring-context\4.3.10.RELEASE\spring-context-4.3.10.RELEASE.jar;D:\mvn_repository\com\sun\mail\javax.mail\1.5.6\javax.mail-1.5.6.jar;D:\mvn_repository\javax\activation\activation\1.1\activation-1.1.jar;D:\mvn_repository\org\springframework\spring-test\4.3.10.RELEASE\spring-test-4.3.10.RELEASE.jar;D:\mvn_repository\jfree\jfreechart\1.0.13\jfreechart-1.0.13.jar;D:\mvn_repository\jfree\jcommon\1.0.16\jcommon-1.0.16.jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-starter\0.1.0\dubbo-spring-boot-starter-0.1.0.jar;D:\mvn_repository\com\alibaba\dubbo\2.5.10\dubbo-2.5.10.jar;D:\mvn_repository\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;D:\mvn_repository\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\mvn_repository\org\apache\zookeeper\zookeeper\3.4.9\zookeeper-3.4.9.jar;D:\mvn_repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;D:\mvn_repository\jline\jline\0.9.94\jline-0.9.94.jar;D:\mvn_repository\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar;D:\mvn_repository\org\apache\curator\curator-framework\2.12.0\curator-framework-2.12.0.jar;D:\mvn_repository\org\apache\curator\curator-client\2.12.0\curator-client-2.12.0.jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-autoconfigure\0.1.0\dubbo-spring-boot-autoconfigure-0.1.0.jar;D:\mvn_repository\com\alibaba\boot\dubbo-spring-boot-actuator\0.1.0\dubbo-spring-boot-actuator-0.1.0.jar;D:\mvn_repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar;D:\mvn_repository\com\google\guava\guava\22.0\guava-22.0.jar;D:\mvn_repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\mvn_repository\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.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.5\httpclient-4.5.5.jar;D:\mvn_repository\org\apache\httpcomponents\httpcore\4.4.6\httpcore-4.4.6.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.47\fastjson-1.2.47.jar;D:\mvn_repository\org\quartz-scheduler\quartz\2.2.1\quartz-2.2.1.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.25\slf4j-api-1.7.25.jar;D:\mvn_repository\org\quartz-scheduler\quartz-jobs\2.2.1\quartz-jobs-2.2.1.jar;D:\mvn_repository\com\jayway\jsonpath\json-path\2.2.0\json-path-2.2.0.jar;D:\mvn_repository\net\minidev\json-smart\2.2.1\json-smart-2.2.1.jar;D:\mvn_repository\net\minidev\accessors-smart\1.1\accessors-smart-1.1.jar;D:\mvn_repository\org\ow2\asm\asm\5.0.3\asm-5.0.3.jar;D:\mvn_repository\org\codehaus\groovy\groovy-all\2.1.6\groovy-all-2.1.6.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.18\lombok-1.16.18.jar;D:\mvn_repository\org\sonatype\plexus\plexus-build-api\0.0.7\plexus-build-api-0.0.7.jar;D:\mvn_repository\org\codehaus\plexus\plexus-utils\1.5.8\plexus-utils-1.5.8.jar;D:\mvn_repository\org\springframework\boot\spring-boot-configuration-processor\1.5.6.RELEASE\spring-boot-configuration-processor-1.5.6.RELEASE.jar;D:\mvn_repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\mvn_repository\org\jsoup\jsoup\1.9.2\jsoup-1.9.2.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-freemarker\1.5.6.RELEASE\spring-boot-starter-freemarker-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter\1.5.6.RELEASE\spring-boot-starter-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-starter-logging\1.5.6.RELEASE\spring-boot-starter-logging-1.5.6.RELEASE.jar;D:\mvn_repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;D:\mvn_repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;D:\mvn_repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;D:\mvn_repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\mvn_repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;D:\mvn_repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;D:\mvn_repository\org\freemarker\freemarker\2.3.26-incubating\freemarker-2.3.26-incubating.jar;D:\mvn_repository\org\springframework\spring-context-support\4.3.10.RELEASE\spring-context-support-4.3.10.RELEASE.jar;D:\mvn_repository\org\springframework\spring-beans\4.3.10.RELEASE\spring-beans-4.3.10.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.0\xmlbeans-2.6.0.jar;D:\mvn_repository\stax\stax-api\1.0.1\stax-api-1.0.1.jar;D:\mvn_repository\com\github\virtuald\curvesapi\1.04\curvesapi-1.04.jar;D:\mvn_repository\com\squareup\okhttp3\okhttp\3.2.0\okhttp-3.2.0.jar;D:\mvn_repository\com\squareup\okio\okio\1.6.0\okio-1.6.0.jar;D:\mvn_repository\org\springframework\spring-core\4.3.10.RELEASE\spring-core-4.3.10.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-devtools\1.5.6.RELEASE\spring-boot-devtools-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot\1.5.6.RELEASE\spring-boot-1.5.6.RELEASE.jar;D:\mvn_repository\org\springframework\boot\spring-boot-autoconfigure\1.5.6.RELEASE\spring-boot-autoconfigure-1.5.6.RELEASE.jar com.longteng.lesson2.service.JsonPathTest 14:18:59.081 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][0]['author'] Nigel Rees 14:18:59.096 [main] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['store']['book'][*]['author'] Nigel Rees Evelyn Waugh Process finished with exit code 0