日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

JsonPath詳細使用教程,你了解多少?

來源: 責編: 時間:2023-09-18 21:42:13 320觀看
導讀Json Path介紹看它的名字你就能知道,這Json Path和JSON文檔有關系,正如XPath之于XML文檔一樣,JsonPath為Json文檔提供了解析能力,通過使用JsonPath,你可以方便的查找節點、獲取想要的數據,JsonPath是Json版的XPath。JsonPat

FsG28資訊網——每日最新資訊28at.com

Json Path介紹

看它的名字你就能知道,這Json Path和JSON文檔有關系,正如XPath之于XML文檔一樣,JsonPath為Json文檔提供了解析能力,通過使用JsonPath,你可以方便的查找節點、獲取想要的數據,JsonPath是Json版的XPath。FsG28資訊網——每日最新資訊28at.com

JsonPath語法

  • 表示文檔的根元素
  • 表示文檔的當前元素
  • .node_name  ['node_name'] 匹配下級節點
  • [index] 檢索數組中的元素
  • [start:end:step] 支持數組切片語法
  • 作為通配符,匹配所有成員
  • .. 子遞歸通配符,匹配成員的所有子元素
  • (<expr>) 使用表達式
  • ?(<boolean expr>)進行數據篩選

XPath與JsonPath比較

XPathFsG28資訊網——每日最新資訊28at.com

JsonPathFsG28資訊網——每日最新資訊28at.com

說明FsG28資訊網——每日最新資訊28at.com

/FsG28資訊網——每日最新資訊28at.com

$FsG28資訊網——每日最新資訊28at.com

文檔根元素FsG28資訊網——每日最新資訊28at.com

.FsG28資訊網——每日最新資訊28at.com

@FsG28資訊網——每日最新資訊28at.com

當前元素FsG28資訊網——每日最新資訊28at.com

/FsG28資訊網——每日最新資訊28at.com

.或[]FsG28資訊網——每日最新資訊28at.com

匹配下級元素FsG28資訊網——每日最新資訊28at.com

..FsG28資訊網——每日最新資訊28at.com

N/AFsG28資訊網——每日最新資訊28at.com

匹配上級元素,JsonPath不支持此操作符FsG28資訊網——每日最新資訊28at.com

//FsG28資訊網——每日最新資訊28at.com

..FsG28資訊網——每日最新資訊28at.com

遞歸匹配所有子元素FsG28資訊網——每日最新資訊28at.com

*FsG28資訊網——每日最新資訊28at.com

*FsG28資訊網——每日最新資訊28at.com

通配符,匹配下級元素FsG28資訊網——每日最新資訊28at.com

@FsG28資訊網——每日最新資訊28at.com

N/AFsG28資訊網——每日最新資訊28at.com

匹配屬性,JsonPath不支持此操作符FsG28資訊網——每日最新資訊28at.com

[]FsG28資訊網——每日最新資訊28at.com

[]FsG28資訊網——每日最新資訊28at.com

下標運算符,根據索引獲取元素,XPath索引從1開始,JsonPath索引從0開始FsG28資訊網——每日最新資訊28at.com

`FsG28資訊網——每日最新資訊28at.com

`FsG28資訊網——每日最新資訊28at.com

[,]FsG28資訊網——每日最新資訊28at.com

N/AFsG28資訊網——每日最新資訊28at.com

[start:end:step]FsG28資訊網——每日最新資訊28at.com

數據切片操作,XPath不支持FsG28資訊網——每日最新資訊28at.com

[]FsG28資訊網——每日最新資訊28at.com

?()FsG28資訊網——每日最新資訊28at.com

過濾表達式FsG28資訊網——每日最新資訊28at.com

N/AFsG28資訊網——每日最新資訊28at.com

()FsG28資訊網——每日最新資訊28at.com

腳本表達式,使用底層腳本引擎,XPath不支持FsG28資訊網——每日最新資訊28at.com

()FsG28資訊網——每日最新資訊28at.com

N/AFsG28資訊網——每日最新資訊28at.com

分組,JsonPath不支持FsG28資訊網——每日最新資訊28at.com

示例

下面是相應的JsonPath的示例,代碼來源于https://goessner/articles/JsonPath/,JSON文檔如下:FsG28資訊網——每日最新資訊28at.com

{	"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		}	}}

解析情況如下:FsG28資訊網——每日最新資訊28at.com

XPathFsG28資訊網——每日最新資訊28at.com

JsonPathFsG28資訊網——每日最新資訊28at.com

ResultFsG28資訊網——每日最新資訊28at.com

/store/book/authorFsG28資訊網——每日最新資訊28at.com

$.store.book[*].authorFsG28資訊網——每日最新資訊28at.com

所有book的author節點FsG28資訊網——每日最新資訊28at.com

//authorFsG28資訊網——每日最新資訊28at.com

$..authorFsG28資訊網——每日最新資訊28at.com

所有author節點FsG28資訊網——每日最新資訊28at.com

/store/*FsG28資訊網——每日最新資訊28at.com

$.store.*FsG28資訊網——每日最新資訊28at.com

store下的所有節點,book數組和bicycle節點FsG28資訊網——每日最新資訊28at.com

/store//priceFsG28資訊網——每日最新資訊28at.com

$.store..priceFsG28資訊網——每日最新資訊28at.com

store下的所有price節點FsG28資訊網——每日最新資訊28at.com

//book[3]FsG28資訊網——每日最新資訊28at.com

$..book[2]FsG28資訊網——每日最新資訊28at.com

匹配第3個book節點FsG28資訊網——每日最新資訊28at.com

//book[last()]FsG28資訊網——每日最新資訊28at.com

$..book[(@.length-1)],或 $..book[-1:]FsG28資訊網——每日最新資訊28at.com

匹配倒數第1個book節點FsG28資訊網——每日最新資訊28at.com

//book[position()<3]FsG28資訊網——每日最新資訊28at.com

$..book[0,1],或 $..book[:2]FsG28資訊網——每日最新資訊28at.com

匹配前兩個book節點FsG28資訊網——每日最新資訊28at.com

//book[isbn]FsG28資訊網——每日最新資訊28at.com

$..book[?(@.isbn)]FsG28資訊網——每日最新資訊28at.com

過濾含isbn字段的節點FsG28資訊網——每日最新資訊28at.com

//book[price<10]FsG28資訊網——每日最新資訊28at.com

$..book[?(@.price<10)]FsG28資訊網——每日最新資訊28at.com

過濾price<10的節點FsG28資訊網——每日最新資訊28at.com

//*FsG28資訊網——每日最新資訊28at.com

$..*FsG28資訊網——每日最新資訊28at.com

遞歸匹配所有子節點FsG28資訊網——每日最新資訊28at.com

可以在http://jsonpath.com/站點進行驗證JsonPath的執行效果。FsG28資訊網——每日最新資訊28at.com

java中使用

pom中引用FsG28資訊網——每日最新資訊28at.com

<dependency>            <groupId>com.jayway.jsonpath</groupId>            <artifactId>json-path</artifactId>            <version>2.4.0</version>        </dependency>

通常是直接使用靜態方法API進行調用,例如:FsG28資訊網——每日最新資訊28at.com

String json = "...";List<String> authors = JsonPath.read(json, "$.store.book[*].author");

但以上方式僅僅適用于解析一次json的情況,如果需要對同一個json解析多次,不建議使用,因為每次read都會重新解析一次json,針對此種情況,建議使用ReadContext、WriteContext,例如:FsG28資訊網——每日最新資訊28at.com

String json = "..."; ReadContext ctx = JsonPath.parse(json); List<String> authorsOfBooksWithISBN = ctx.read("$.store.book[?(@.isbn)].author"); List<Map<String, Object>> expensiveBooks = JsonPath                            .using(configuration)                            .parse(json)                            .read("$.store.book[?(@.price > 10)]", List.class);

本文鏈接:http://www.www897cc.com/showinfo-26-10484-0.htmlJsonPath詳細使用教程,你了解多少?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 零拷貝并非萬能解決方案:重新定義數據傳輸的效率極限

下一篇: Springboot — 用更優雅的方式發HTTP請求(RestTemplate詳解)

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 淮阳县| 赤峰市| 靖安县| 灵宝市| 乌鲁木齐县| 玉龙| 延安市| 新营市| 黔江区| 绥棱县| 诸暨市| 隆德县| 九龙县| 和龙市| 如皋市| 象山县| 前郭尔| 庆云县| 阜平县| 卢氏县| 香格里拉县| 灵山县| 平利县| 如东县| 汪清县| 犍为县| 博湖县| 武定县| 千阳县| 长子县| 宁武县| 北安市| 四子王旗| 衢州市| 库尔勒市| 山丹县| 岚皋县| 竹溪县| 泽普县| 涟源市| 保定市|