JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容。下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它。
一.首先需要依赖的jar包
二.因为编译的时候会报log4j的警报,所以需要在项目的src目录下新建log4j.properties文件,内容如下:
log4j.rootLogger=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
三.准备一个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 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } }, "expensive": 10 }
四.我们从代码中分析用法,代码如下
package com.jsonpath; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Iterator; import java.util.List; import java.util.Map; import net.minidev.json.JSONArray; import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.ReadContext; /** * @author QiaoJiafei * @version 创建时间:2016年3月2日 下午3:37:42 * 类说明 */ public class TestJsonPath { public static void main(String[] args) { // TODO Auto-generated method stub String sjson = readtxt(); //String sjson = "{\"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},{\"category\": \"fiction\",\"author\": \"Herman Melville\",\"title\": \"Moby Dick\",\"isbn\": \"0-553-21311-3\",\"price\": 8.99},{\"category\": \"fiction\",\"author\": \"J. R. R. Tolkien\",\"title\": \"The Lord of the Rings\",\"isbn\": \"0-395-19395-8\",\"price\": 22.99}],\"bicycle\": {\"color\": \"red\",\"price\": 19.95}},\"expensive\": 10}"; /* print("--------------------------------------getJsonValue0--------------------------------------"); getJsonValue0(sjson); print("--------------------------------------getJsonValue1--------------------------------------"); getJsonValue1(sjson); print("--------------------------------------getJsonValue2--------------------------------------"); getJsonValue2(sjson); print("--------------------------------------getJsonValue3--------------------------------------"); getJsonValue3(sjson); print("--------------------------------------getJsonValue4--------------------------------------"); getJsonValue4(sjson); */ print("--------------------------------------getJsonValue--------------------------------------"); getJsonValue(sjson); } private static String readtxt() { // TODO Auto-generated method stub StringBuilder sb = new StringBuilder(); try { FileReader fr = new FileReader("D:/workspace/PressureTest/json.txt"); BufferedReader bfd = new BufferedReader(fr); String s = ""; while((s=bfd.readLine())!=null) { sb.append(s); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(sb.toString()); return sb.toString(); } private static void getJsonValue(String json) { //The authors of all books:获取json中store下book下的所有author值 Listauthors1 = JsonPath.read(json, "$.store.book[*].author"); //All authors:获取所有json中所有author的值 List authors2 = JsonPath.read(json, "$..author"); //All things, both books and bicycles //authors3返回的是net.minidev.json.JSONArray:获取json中store下的所有value值,不包含key,如key有两个,book和bicycle List
1.首先我们看getJsonValue()这个方法的输出结果:
--------------------------------------getJsonValue-------------------------------------- **************authors1************** Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien **************authors2************** Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien **************authors3************** [{"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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] {color=red, price=19.95} **************authors4************** 8.95 12.99 8.99 22.99 19.95 **************authors5************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors6************** {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} **************authors7************** {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} **************authors8************** {category=fiction, author=Evelyn Waugh, title=Sword of Honour, price=12.99} **************authors9************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors10************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors11************** {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} **************authors12************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors13************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} **************authors14************** {category=reference, author=Nigel Rees, title=Sayings of the Century, price=8.95} **************authors15************** {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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}], bicycle={color=red, price=19.95}} 10 [{"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},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}] {color=red, price=19.95} {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} {category=fiction, author=Herman Melville, title=Moby Dick, isbn=0-553-21311-3, price=8.99} {category=fiction, author=J. R. R. Tolkien, title=The Lord of the Rings, isbn=0-395-19395-8, price=22.99} reference Nigel Rees Sayings of the Century 8.95 fiction Evelyn Waugh Sword of Honour 12.99 fiction Herman Melville Moby Dick 0-553-21311-3 8.99 fiction J. R. R. Tolkien The Lord of the Rings 0-395-19395-8 22.99 red 19.95 **************authors16************** 4
2.然后看getJsonValue0()、getJsonValue1()、getJsonValue2()、getJsonValue3()、getJsonValue4()的输出结果
--------------------------------------getJsonValue0-------------------------------------- Nigel Rees Evelyn Waugh Herman Melville J. R. R. Tolkien --------------------------------------getJsonValue1-------------------------------------- Nigel Rees Evelyn Waugh --------------------------------------getJsonValue2-------------------------------------- Herman Melville J. R. R. Tolkien ****************Map**************** **** category=fiction author=Evelyn Waugh title=Sword of Honour price=12.99 **** category=fiction author=J. R. R. Tolkien title=The Lord of the Rings isbn=0-395-19395-8 price=22.99 --------------------------------------getJsonValue3-------------------------------------- Nigel Rees --------------------------------------getJsonValue4-------------------------------------- ****************books1**************** **** category=fiction author=Herman Melville title=Moby Dick isbn=0-553-21311-3 price=8.99 ****************books2**************** **** category=fiction author=Herman Melville title=Moby Dick isbn=0-553-21311-3 price=8.99
3.相应的解释已经在代码中注释,更多的用法详见官网链接:https://github.com/jayway/JsonPath
4.如果只是想简单的处理json,也可以使用JSONObject和JSONArray,具体用法详见我的这篇文章:http://www.cnblogs.com/qiaoyeye/p/4730930.html
----------更新2016年08月19日10:06:09-------
You can use &&
and ||
to combine multiple predicates [?(@.price < 10 && @.category == 'fiction')]
, [?(@.category == 'reference' || @.price > 10)]
这个好厉害,可以这样用
JsonPath.read(json, "$..farePrices[?(@.priceType == 'SalePrice' && @.passengerType == 'ADULT')].amount");