欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python的jsonpath庫使用方法實(shí)例

 更新時間:2023年08月21日 10:39:54   作者:shines_m  
這篇文章主要介紹了Python的jsonpath庫使用方法實(shí)例,接口返回的jsonn數(shù)據(jù),需要取值后斷言,一般我們是使用jsonpath來提取接口返回的數(shù)據(jù) ,JsonPath是一種信息抽取類庫,是從JSON文檔中抽取指定信息的工具,,需要的朋友可以參考下

一、簡介

接口返回的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合成一個list的方法小結(jié)

    python中,list這種數(shù)據(jù)結(jié)構(gòu)很常用到,如果兩個或者多個list結(jié)構(gòu)相同,內(nèi)容類型相同,我們通常會將兩個或者多個list合并成一個,這樣我們再循環(huán)遍歷的時候就可以一次性處理掉了
    2019-05-05
  • pyqt5簡介及安裝方法介紹

    pyqt5簡介及安裝方法介紹

    這篇文章主要介紹了pyqt5簡介及安裝方法介紹,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 將python flask項(xiàng)目打包成可以運(yùn)行的軟件的全過程(包含報錯解決)

    將python flask項(xiàng)目打包成可以運(yùn)行的軟件的全過程(包含報錯解決)

    這篇文章主要給大家介紹了將python flask項(xiàng)目打包成可以用運(yùn)行的軟件(包含報錯解決),文中通過代碼示例和圖文結(jié)合講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下
    2024-02-02
  • python實(shí)現(xiàn)簡單的超市商品銷售管理系統(tǒng)

    python實(shí)現(xiàn)簡單的超市商品銷售管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市商品銷售管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python 3.8中實(shí)現(xiàn)functools.cached_property功能

    Python 3.8中實(shí)現(xiàn)functools.cached_property功能

    這篇文章主要介紹了Python 3.8中實(shí)現(xiàn)functools.cached_property功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • python的類class定義及其初始化方式

    python的類class定義及其初始化方式

    這篇文章主要介紹了python的類class定義及其初始化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • python對ip地址進(jìn)行排序、分類的方法詳解

    python對ip地址進(jìn)行排序、分類的方法詳解

    這篇文章主要介紹了python對ip地址進(jìn)行排序、分類的方法詳解,IP協(xié)議全稱為“網(wǎng)際互連協(xié)議Internet?Protocol”,IP協(xié)議是TCP/IP體系中的網(wǎng)絡(luò)層協(xié)議,需要的朋友可以參考下
    2023-07-07
  • Python實(shí)現(xiàn)拼接多張圖片的方法

    Python實(shí)現(xiàn)拼接多張圖片的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)拼接多張圖片的方法,以具體的實(shí)際應(yīng)用引出Python拼接圖片的思路并給出了實(shí)現(xiàn)的具體方法,需要的朋友可以參考下
    2014-12-12
  • python爬取cnvd漏洞庫信息的實(shí)例

    python爬取cnvd漏洞庫信息的實(shí)例

    今天小編就為大家分享一篇python爬取cnvd漏洞庫信息的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • Python中函數(shù)的基本定義與調(diào)用及內(nèi)置函數(shù)詳解

    Python中函數(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

最新評論