亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

介紹

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

安裝

pip install jsonpath

語法

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

語法規(guī)則

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

舉例說明

獲取書架上的書進行操作

  • 數(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 實戰(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.*所有的書籍
$.store…pricestore 中的所有的內(nèi)容的價格
$…book[2]第三本書
$…book[(@.length-1)] 或 $…book[-1:]最后一本書
$…book[0,1] 或 $…book[:2]前兩本書
$…book[?(@.isbn)]獲取有 isbn 的所有數(shù)
$…book[?(@.price<100)]獲取價格<100的所有的書
$…*獲取所有的數(shù)據(jù)

在 python 中使用

如下代碼都用單元測試來舉例,具體代碼鏈接:去這里

  • 設置測試結(jié)構(gòu)
def setUp(self):
  """
  前置設置
  @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 實戰(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é)點的作者

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

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

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

獲取所有價格

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

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

取出第三本書的所有信息

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

取出價格大于70塊的所有書本

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

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

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

def test_mongo_data(self):
    """
    獲取mongo中的復雜結(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("獲取某一個字字段的列表:", change_list)
    pass

效果:

在這里插入圖片描述

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

相關文章

最新評論