Python的jsonpath庫(kù)使用方法實(shí)例
一、簡(jiǎn)介
接口返回的json數(shù)據(jù),需要取值后斷言,一般我們是使用jsonpath來(lái)提取接口返回的數(shù)據(jù)
JsonPath是一種信息抽取類庫(kù),是從JSON文檔中抽取指定信息的工具。
JsonPath相當(dāng)于是Xpath 。
安裝
pip install jsonpath
二、語(yǔ)法說(shuō)明
JsonPath解析的內(nèi)容必須是字典格式,通過(guò)JsonPath獲得的內(nèi)容,會(huì)以list的形式進(jìn)行返回 。
如果表達(dá)式出現(xiàn)錯(cuò)誤,則會(huì)返回False(布爾類型的值)
jsonpath表達(dá)式的基本格式規(guī)范:
符號(hào) | 描述 |
$ | 表示根節(jié)點(diǎn),也是所有jsonpath表達(dá)式的開(kāi)始 |
. | 表示獲取子節(jié)點(diǎn) |
.. | 表示獲取所有符合條件的內(nèi)容 |
* | 代表所有的元素節(jié)點(diǎn) |
[] | 表示迭代器的標(biāo)示(可以用于處理下標(biāo)等情況) |
[,] | 表示多個(gè)結(jié)果的選擇 |
?() | 表示過(guò)濾操作 |
@ | 表示當(dāng)前節(jié)點(diǎn) |
三、代碼示例
import jsonpath data = { "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 } } } # 獲取所有book的價(jià)格 price = jsonpath.jsonpath(data, '$.store.book[*].price') print(price) >> [8.95, 12.99, 8.99, 22.99] # 獲取部分book節(jié)點(diǎn)下的price節(jié)點(diǎn)值,可支持切片操作 res1 = jsonpath.jsonpath(data, '$.store.book[0,1].price') res2 = jsonpath.jsonpath(data, '$.store.book[0:2].price') print(res1) print(res2) >> [8.95, 12.99] >> [8.95, 12.99] # 獲取最后一本書(shū)的價(jià)格(不能通過(guò)[-1]這種方式獲?。? res3 = jsonpath.jsonpath(data, '$.store.book[-1:].price') print(res3) >> [22.99] # 獲取store節(jié)點(diǎn)下所有price節(jié)點(diǎn)的值 res4 = jsonpath.jsonpath(data, '$.store...price') res5 = jsonpath.jsonpath(data, '$..price') print(res4) print(res5) >> [8.95, 12.99, 8.99, 22.99, 19.95] >> [8.95, 12.99, 8.99, 22.99, 19.95] # jsonpath解析錯(cuò)誤,返回bool類型數(shù)據(jù)false res6 = jsonpath.jsonpath(data, '$.store.book111') print(res6) >> False # 獲取價(jià)格大于10的所有書(shū)籍信息 book = jsonpath.jsonpath(data, '$..book[?(@.price>10)]') print(book) >> [{'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}] # 獲取book的價(jià)格大于10的prcice price = jsonpath.jsonpath(data, '$..book[?(@.price>10)].price') print(price) >> [12.99, 22.99] # 過(guò)濾所有帶有 isbn編號(hào)的書(shū)籍信息 isbn = jsonpath.jsonpath(data, '$..book[?(@.isbn)]') print(isbn) >> [{'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}]
到此這篇關(guān)于Python的jsonpath庫(kù)使用方法實(shí)例的文章就介紹到這了,更多相關(guān)Python的jsonpath庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中將兩個(gè)或多個(gè)list合成一個(gè)list的方法小結(jié)
python中,list這種數(shù)據(jù)結(jié)構(gòu)很常用到,如果兩個(gè)或者多個(gè)list結(jié)構(gòu)相同,內(nèi)容類型相同,我們通常會(huì)將兩個(gè)或者多個(gè)list合并成一個(gè),這樣我們?cè)傺h(huán)遍歷的時(shí)候就可以一次性處理掉了2019-05-05將python flask項(xiàng)目打包成可以運(yùn)行的軟件的全過(guò)程(包含報(bào)錯(cuò)解決)
這篇文章主要給大家介紹了將python flask項(xiàng)目打包成可以用運(yùn)行的軟件(包含報(bào)錯(cuò)解決),文中通過(guò)代碼示例和圖文結(jié)合講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02python實(shí)現(xiàn)簡(jiǎn)單的超市商品銷售管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市商品銷售管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Python 3.8中實(shí)現(xiàn)functools.cached_property功能
這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05python對(duì)ip地址進(jìn)行排序、分類的方法詳解
這篇文章主要介紹了python對(duì)ip地址進(jìn)行排序、分類的方法詳解,IP協(xié)議全稱為“網(wǎng)際互連協(xié)議Internet?Protocol”,IP協(xié)議是TCP/IP體系中的網(wǎng)絡(luò)層協(xié)議,需要的朋友可以參考下2023-07-07Python中函數(shù)的基本定義與調(diào)用及內(nèi)置函數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Python中函數(shù)的基本定義與調(diào)用及內(nèi)置函數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05