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

python中jsonpath的使用小結(jié)

 更新時(shí)間:2024年03月20日 09:13:28   作者:hjc_042043  
JsonPath是一種信息抽取類(lèi)庫(kù),是從JSON文檔中抽取指定信息的工具,提供多種語(yǔ)言實(shí)現(xiàn)版本,本文主要介紹了python中jsonpath的使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下

介紹

JSONPath能在復(fù)雜的JSON數(shù)據(jù)中 查找和提取所需的信息,它是一種功能強(qiáng)大的查詢(xún)語(yǔ)言,可以通過(guò)簡(jiǎn)單的表達(dá)式來(lái)快速準(zhǔn)確地定位和提取JSON數(shù)據(jù)。本文將介紹JSONPath的基本語(yǔ)法和用法,并為您展示如何封裝和使用JSONPath方法來(lái)處理和操作JSON數(shù)據(jù)。
JSONPath類(lèi)似于XPath提供了一種更簡(jiǎn)潔、靈活和高效的方式來(lái)查詢(xún)、定位和提取JSON數(shù)據(jù)中的內(nèi)容

安裝

pip install jsonpath

語(yǔ)法

from jsonpath import jsonpath
res = jsonpath(dict,'jsonpath語(yǔ)法規(guī)則字符串')

語(yǔ)法規(guī)則

JsonPath描述
$根節(jié)點(diǎn)元素,最外層的大括號(hào)
@當(dāng)前節(jié)點(diǎn)元素
. 或 []絕對(duì)路徑,取子節(jié)點(diǎn)元素
相對(duì)路徑,內(nèi)部的任意位置,選擇符合條件的子孫節(jié)點(diǎn)元素
*匹配所有元素節(jié)點(diǎn)
[]迭代器提示(可以在里邊做簡(jiǎn)單的迭代操作,如數(shù)組下標(biāo),根據(jù)內(nèi)容篩選)
[,]支持迭代器中做多選
?()支持過(guò)濾操作
()支持表達(dá)式計(jì)算

舉例說(shuō)明

獲取書(shū)架上的書(shū)進(jìn)行操作

  • 數(shù)據(jù)結(jié)構(gòu):
book_dict = {
    "store":{
        "book":[
            {
                "name": "java 核心編程1",
                "price": "65",
                "isbn": "IS002598934",
                "author" : "周立新"
            },
            {
                "name": "java 核心編程2",
                "price": "64",
                "isbn": "IS876430456",
                "author": "周立新"
            },
            {
                "name" : "java 編程思想",
                "price": "120",
                "isbn": "IS256709873",
                "author": "Bruce Eckel"
            },
            {
                "name" : "RabbitMq 實(shí)戰(zhàn)指南",
                "price": "79",
                "isbn": "IS987623450",
                "author": "朱忠華"
            },
            {
                "name" : "圖解 TCP/IP",
                "price": "69",
                "isbn": "IS9787354679",
                "author": "竹下隆史"
            }
        ]
    }
}
  • 結(jié)構(gòu)解析例子
JsonPath結(jié)果
$.store.book[*].authorstore 中的所有的 book的作者
$…store.*所有的書(shū)籍
$.store…pricestore 中的所有的內(nèi)容的價(jià)格
$…book[2]第三本書(shū)
$…book[(@.length-1)] 或 $…book[-1:]最后一本書(shū)
$…book[0,1] 或 $…book[:2]前兩本書(shū)
$…book[?(@.isbn)]獲取有 isbn 的所有數(shù)
$…book[?(@.price<100)]獲取價(jià)格<100的所有的書(shū)
$…*獲取所有的數(shù)據(jù)

在 python 中使用

如下代碼都用單元測(cè)試來(lái)舉例,具體代碼鏈接:去這里

  • 設(shè)置測(cè)試結(jié)構(gòu)
def setUp(self):
  """
  前置設(shè)置
  @return:
  """
  self.book_dict = {
      "store": {
          "book": [
              {
                  "name": "java 核心編程1",
                  "price": 65,
                  "isbn": "IS002598934",
                  "author": "周立新"
              },
              {
                  "name": "java 核心編程2",
                  "price": 64,
                  "isbn": "IS876430456",
                  "author": "周立新"
              },
              {
                  "name": "java 編程思想",
                  "price": 120,
                  "isbn": "IS256709873",
                  "author": "Bruce Eckel"
              },
              {
                  "name": "RabbitMq 實(shí)戰(zhàn)指南",
                  "price": 79,
                  "isbn": "IS987623450",
                  "author": "朱忠華"
              },
              {
                  "name": "圖解 TCP/IP",
                  "price": 69,
                  "isbn": "IS9787354679",
                  "author": "竹下隆史"
              }
          ]
      }
  }
  pass

獲取所有結(jié)構(gòu)

def test_all(self):
    """
    獲取所有的結(jié)構(gòu)
    @return:
    """
    dict_data = jsonpath(self.book_dict, '$..*')
    print("查看dict_data支持的屬性和方法:", dir(dict_data))

    try:
        for item in dict_data[0]['book']:
            # 美化打印
            pprint(item)
    except Exception as e:
        print(e)
        pass

所有子節(jié)點(diǎn)的作者

def test_sub_author(self):
    """
    獲取所有子節(jié)點(diǎn)的作者
    @return:
    """
    all_author = jsonpath(self.book_dict, '$.store.book[*].author')
    print(all_author)
    pass

獲取所有子孫節(jié)點(diǎn)

def test_sub_all(self):
    """
    獲取所有子孫節(jié)點(diǎn)
    @return:
    """
    sub_all = jsonpath(self.book_dict, '$..store.*')
    print(sub_all)
    pass

獲取所有價(jià)格

def test_price_all(self):
    """
    獲取子節(jié)點(diǎn)的所有價(jià)格
    @return:
    """
    price_all = jsonpath(self.book_dict, '$..price')
    print("所有的價(jià)格:", price_all)

    price_sum = sum([int(price) for price in price_all])
    print(f"價(jià)格之和:{price_sum}")
    pass

取出第三本書(shū)的所有信息

 def test_book_item(self):
     """
     取出第三本書(shū)的所有信息
     @return:
     """
     book_item = jsonpath(self.book_dict, '$..book[2]')
     print(book_item)
     pass

取出價(jià)格大于70塊的所有書(shū)本

def test_book_filter(self):
    book_count = jsonpath(self.book_dict, '$..book[?(@.price>70)]')
    print(book_count)

從mongodb 中取數(shù)據(jù)的示例

遇到復(fù)雜的結(jié)構(gòu)可以使用 pprint()來(lái)美化打印

def test_mongo_data(self):
    """
    獲取mongo中的復(fù)雜結(jié)構(gòu)的數(shù)據(jù)
    @return:
    """
    dict_data = MongoPool().project_8.coffer_check_data.find_one({"_id": "629dbfe615e74466831b5ce2"})
    # 美化打印
    pprint(dict_data)
    change_list = jsonpath(dict_data, '$..change')
    print("獲取某一個(gè)字字段的列表:", change_list)
    pass

效果:

在這里插入圖片描述

到此這篇關(guān)于python中jsonpath的使用小結(jié)的文章就介紹到這了,更多相關(guān)python jsonpath使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論