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