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

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

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

一、簡(jiǎn)介

接口返回的json數(shù)據(jù),需要取值后斷言,一般我們是使用jsonpath來提取接口返回的數(shù)據(jù)

JsonPath是一種信息抽取類庫(kù),是從JSON文檔中抽取指定信息的工具。

JsonPath相當(dāng)于是Xpath 。

安裝

pip install jsonpath

二、語(yǔ)法說明

JsonPath解析的內(nèi)容必須是字典格式,通過JsonPath獲得的內(nèi)容,會(huì)以list的形式進(jìn)行返回 。

如果表達(dá)式出現(xiàn)錯(cuò)誤,則會(huì)返回False(布爾類型的值)

jsonpath表達(dá)式的基本格式規(guī)范:

符號(hào)描述
$表示根節(jié)點(diǎn),也是所有jsonpath表達(dá)式的開始
.表示獲取子節(jié)點(diǎn)
..表示獲取所有符合條件的內(nèi)容
*代表所有的元素節(jié)點(diǎn)
[]表示迭代器的標(biāo)示(可以用于處理下標(biāo)等情況)
[,]表示多個(gè)結(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的價(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]
# 獲取最后一本書的價(jià)格(不能通過[-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的所有書籍信息
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]
# 過濾所有帶有 isbn編號(hào)的書籍信息
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)文章

最新評(píng)論