Python Json讀寫(xiě)操作之JsonPath用法詳解
Python Json讀寫(xiě)操作_JsonPath用法詳解
1. 介紹
JSONPath是一種信息抽取類(lèi)庫(kù),是從JSON文檔中抽取指定信息的工具,提供多種語(yǔ)言實(shí)現(xiàn)版本,包括Javascript、Python、PHP和Java。
JSONPath的安裝方法如下:pip install jsonpath
JSONPath語(yǔ)法和XPATH語(yǔ)法對(duì)比,JSON結(jié)構(gòu)清晰,可讀性高,復(fù)雜度低,非常容易匹配。JSONPath的語(yǔ)法與Xpath類(lèi)似,如下表所示為JSONPath與XPath語(yǔ)法對(duì)比:

2. 代碼示例
bookJson = {
"store": {
"book":[
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "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
}
}
}
變量bookJson中已經(jīng)包含了這段JSON字符串,可通過(guò)以下代碼反序列化得到JSON對(duì)象:
books=json.loads(bookJson)
1)查看store下的bicycle的color屬性:
checkurl = "$.store.bicycel.color" print(jsonpath.jsonpath(books, checkurl)) # 輸出:['red']
2)輸出book節(jié)點(diǎn)中包含的所有對(duì)象:
checkurl = "$.store.book[*]" object_list=jsonpath.jsonpath(books, checkurl) print(object_list)
3)輸出book節(jié)點(diǎn)的第一個(gè)對(duì)象:
checkurl = "$.store.book[0]" obj = jsonpath.jsonpath(books, checkurl) print(obj) # 輸出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]
4)輸出book節(jié)點(diǎn)中所有對(duì)象對(duì)應(yīng)的屬性title值:
checkurl = "$.store.book[*].title" titles = jsonpath.jsonpath(books, checkurl) print(titles) # 輸出: ['Sayings of the Century', 'The Lord of the Rings']
5)輸出book節(jié)點(diǎn)中category為fiction的所有對(duì)象:
checkurl = "$.store.book[?(@.category=='fiction')]”
books=jsonpath.jsonpath(books, checkurl)
print(books)
# 輸出:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lordof the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]6)輸出book節(jié)點(diǎn)中所有價(jià)格小于10的對(duì)象:
checkurl="$.store.book[?(@.price<10)]"
books = jsonpath.jsonpath(books, checkurl)
print(books)
# 輸出: [{'category': 'reference', 'author': 'Nigel Rees', 'title':'Sayings of the Century', 'price': 8.95}]7)輸出book節(jié)點(diǎn)中所有含有isb的對(duì)象:
checkurl = "$.store.book[?(@.isb)]"
books = jsonpath.jsonpath(books,checkurl)
print(books)
# 輸出: [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]3. 參考
【1】https://blog.csdn.net/fallenjency/article/details/123276600
到此這篇關(guān)于Python Json讀寫(xiě)操作之JsonPath用法詳解的文章就介紹到這了,更多相關(guān)Python JsonPath用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python datatime庫(kù)語(yǔ)法使用詳解
這篇文章主要介紹了Python datatime庫(kù)語(yǔ)法使用詳解,datetime模塊用于是date和time模塊的合集,文章圍繞相關(guān)資料展開(kāi)詳情,感興趣的小伙伴可以擦參考一下2022-07-07
Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動(dòng)態(tài)圖)
這篇文章主要介紹了Python圖片轉(zhuǎn)gif方式(將靜態(tài)圖轉(zhuǎn)化為分塊加載的動(dòng)態(tài)圖),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Python3之字節(jié)串bytes與字節(jié)數(shù)組bytearray的使用詳解
今天小編就為大家分享一篇Python3之字節(jié)串bytes與字節(jié)數(shù)組bytearray的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python真題案例之小學(xué)算術(shù)?階乘精確值?孿生素?cái)?shù)?6174問(wèn)題詳解
這篇文章主要介紹了python實(shí)操案例練習(xí),本文給大家分享的案例中主要任務(wù)有小學(xué)生算術(shù)、階乘的精確值、孿生素?cái)?shù)、6174問(wèn)題,需要的小伙伴可以參考一下2022-03-03
使用Python實(shí)現(xiàn)PDF與SVG互轉(zhuǎn)
SVG(可縮放矢量圖形)和PDF(便攜式文檔格式)是兩種常見(jiàn)且廣泛使用的文件格式,本文將詳細(xì)介紹如何使用?Python?實(shí)現(xiàn)?SVG?和?PDF?之間的相互轉(zhuǎn)換,感興趣的可以了解下2025-02-02

