AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

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

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

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

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

Json Path介紹

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

JsonPath語法

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

XPath與JsonPath比較

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

示例

下面是相應的JsonPath的示例,代碼來源于https://goessner/articles/JsonPath/,JSON文檔如下:ADd28資訊網——每日最新資訊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		}	}}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

java中使用

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

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

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

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

但以上方式僅僅適用于解析一次json的情況,如果需要對同一個json解析多次,不建議使用,因為每次read都會重新解析一次json,針對此種情況,建議使用ReadContext、WriteContext,例如:ADd28資訊網——每日最新資訊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.tebozhan.com/showinfo-26-10484-0.htmlJsonPath詳細使用教程,你了解多少?

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

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

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

標簽:
  • 熱門焦點
Top