一、fastjsonjsonpath簡介
fastjsonjsonpath是一個基於fastjson的json路徑解析工具,可以根據給定的json路徑來查詢、獲取json對象中的值。此工具具有以下優勢:
1、速度快,性能較優;
2、支持無限層級的json對象路徑查找;
3、使用簡單,易於上手;
4、可方便地與fastjson結合使用。
二、基本用法
1、導入fastjsonjsonpath包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjsonjsonpath</artifactId> <version>0.8.0</version> </dependency>
2、創建待解析的json字符串
String jsonStr = "{ \"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 } ], \"bicycle\": { \"color\": \"red\", \"price\": 19.95 } } }";
3、根據需要解析的路徑,使用JsonPath類的靜態方法read解析json字符串,並使用read方法返回的Object進行數據獲取
Object result = JsonPath.read(jsonStr, "$.store.book[1].author"); System.out.println(result.toString()); //輸出:Evelyn Waugh
三、常用語法
①.基本語法
1、$表示根節點;
2、@表示當前節點;
3、.表示逐層進入下一級;
4、..表示不限制層級;
5、*表示匹配所有節點;
6、[]表示篩選節點。
例如,$表示根節點,$.store表示根節點下的store節點,$.store.*表示store節點下的所有子節點,$.store..book[0]表示store節點和所有子孫節點中的第一個book節點。
②.篩選語法
篩選語法用於篩選滿足特定條件的節點。
例如,$.store.book[?(@.price < 10)]表示選取store節點下,價格小於10的book節點。
③.函數語法
函數語法用於進行節點值的計算和轉換。
例如,$.store.book[0].price.floor()表示選取store節點中第一個book節點的價格,並向下取整。
四、高級用法
JsonPath支持許多高級用法,如動態篩選、多條件篩選、嵌套查詢等。
①.動態篩選
JsonPath支持使用Java代碼動態生成篩選條件。
String jsonStr = "{ \"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 } ], \"bicycle\": { \"color\": \"red\", \"price\": 19.95 } } }"; //定義篩選條件 String filter = "$.store.book[?(@.price < %s)].price"; //動態生成篩選條件,獲取價格小於10的book節點的價格 Object result = JsonPath.read(jsonStr, String.format(filter, 10)); System.out.println(result.toString()); //輸出:[8.95]
②.多條件篩選
JsonPath支持同時使用多個條件進行篩選。
String jsonStr = "{ \"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 } ], \"bicycle\": { \"color\": \"red\", \"price\": 19.95 } } }"; //定義篩選條件 String filter = "$.store.book[?(@.price < %s && @.author == '%s')].title"; //動態生成篩選條件,獲取價格小於10且作者是Nigel Rees的book節點的書名 Object result = JsonPath.read(jsonStr, String.format(filter, 10, "Nigel Rees")); System.out.println(result.toString()); //輸出:["Sayings of the Century"]
③.嵌套查詢
JsonPath支持在篩選條件中使用嵌套查詢。
String jsonStr = "{ \"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 } ], \"bicycle\": { \"color\": \"red\", \"price\": 19.95 } } }"; //定義篩選條件 String filterOuter = "$.store.book[?(@.price < %s && @.author == '%s')].title"; String filterInner = "$..book[?(@.price < %s && @.author == '%s')].title"; //動態生成嵌套查詢條件,獲取價格小於10且作者是Nigel Rees的book節點的書名(第二個查詢條件需要嵌套使用) Object result = JsonPath.parse(jsonStr).read(String.format(filterOuter, 10, "Nigel Rees")).read(String.format(filterInner, 10, "Evelyn Waugh")); System.out.println(result.toString()); //輸出:["Sword of Honour"]
五、綜合實例
下面的代碼演示了如何根據一個json配置文件的路徑和對應的key,動態獲取某個字段的值。
首先,我們定義一個json配置文件:
{ "database": { "localhost": { "username": "user", "password": "pass" } } }
然後,我們定義一個方法,根據所需key和json配置文件路徑,動態獲取字段的值:
public static String getJsonValue(String jsonStr, String path, String key) { //根據json配置文件路徑和所需key,拼接出完整的jsonPath表達式 String jsonPath = "$." + path + ".[key]"; jsonPath = jsonPath.replace("[key]", "." + key); //使用jsonPath查詢,返回結果 Object result = JsonPath.read(jsonStr, jsonPath); return result.toString(); }
調用該方法,獲取json配置文件中localhost節點下的username字段:
String jsonStr = "{ \"database\": { \"localhost\": { \"username\": \"user\", \"password\": \"pass\" } } }"; String path = "database.localhost"; String key = "username"; String result = getJsonValue(jsonStr, path, key); System.out.println(result); //輸出:user
六、總結
本文介紹了fastjsonjsonpath的基本用法和常用語法,以及一些高級用法,包括動態篩選、多條件篩選和嵌套查詢。使用fastjsonjsonpath可以快速、方便地對json對象進行查詢和獲取,是一個非常優秀的json路徑解析工具。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/185621.html