日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python深层解析json数据之JsonPath

發布時間:2025/3/15 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python深层解析json数据之JsonPath 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ?我們在做接口自動化時,一般接口響應的都是json數據體,對響應數據進行提取使用或斷言,當數據量很大或層級很深時,就會變得很麻煩,于是就可以用到jsonpath模塊,解決json路徑深取值難的問題。

一、?jsonpath介紹

用來解析多層嵌套的json數據;JsonPath 是一種信息抽取類庫,是從JSON文檔中抽取指定信息的工具,提供多種語言實現版本,包括:Javascript, Python, PHP 和 Java。

官網文檔http://goessner.net/articles/JsonPath

二、安裝使用

pip install jsonpath

import jsonpath

jp=jsonpath.jsonpath(response, '$..key_name')

“$”表示最外層的{},“..”表示模糊匹配,輸出結果是list,當傳入不存在的key_name時,程序會返回false

三、JsonPath與Xpath語法對比

Json結構清晰,可讀性高,復雜度低,非常容易匹配,下表中對應了XPath的用法

四、案例

案例1: import jsonpathjson_data1={"error_code": 0,"stu_info": [{"id": 1001,"name": "橙汁","sex": "女","age": 33,"addr": "上海市浦東新區城南路500弄105號","grade": "天蝎座","phone": "18300000000","gold": 10896,"info":{"card":6123492929293838,"bank_name":'招商銀行'}},{"id": 1002,"name": "橙子","sex": "男","age": 40,"addr": "陜西省西安市曲江新區","grade": "獅子座","phone": "18200000000","gold": 100}] }# 獲取json_data1中所有phone字段值 print(jsonpath.jsonpath(json_data1,'$..phone')) ['18300000000', '18200000000']# 獲取json_data1中所有bank_name字段值 print(jsonpath.jsonpath(json_data1,'$..bank_name')) ['招商銀行']# 獲取json_data1中info下所有字段值 print(jsonpath.jsonpath(json_data1,'$..info.*')) [6123492929293838, '招商銀行']# 獲取json_data1中stu_info中第1個元素中grade字段值 print(jsonpath.jsonpath(json_data1,'$.stu_info.[0].grade')) ['天蝎座'] # 案例2 import jsonpathjson_data2={"body": {"cityId": 605,"title": "本周行情聚焦","weekDate": "07.06-07.12","wechat": "tuboshikefu02","button": "每月專家行情解析,免費參加","reportList": [{"cityId": 605,"skuId": 13158,"skuFullName": "上海不夠賣小區榜","spuId": 20130,"spuName": "上海不夠賣小區榜","categoryName": "爆款報告","skuPrice": 1100,"spuType": 1,"h5Url": "https://m.2boss.cn/trade/3051420769646018571885?skuIdAlias=3051483295561809939455","reportTitle": "上海不夠賣小區榜","reportUrl": "https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592899866409","reportContent": "【最新6月榜】各區最火熱的小區,全新數據,那些才是真正最火的小區?各區庫存去化周期排行榜。","orderTime": 1593507849000}, {"cityId": 605,"skuId": 13162,"skuFullName": "上海漲漲漲小區榜","spuId": 20134,"spuName": "上海漲漲漲小區榜","categoryName": "爆款報告","skuPrice": 1200,"spuType": 1,"h5Url": "https://m.2boss.cn/trade/3051443826783682574415?skuIdAlias=3051495335856701455454","reportTitle": "上海漲漲漲小區榜","reportUrl": "https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592900443037","reportContent": "【最新6月榜】各區最有可能上漲小區,全新數據,幫你選最能升值的小區,各區房東溢價系數排行榜。","orderTime": 1592980444000},{"id":111,"name":"橙子"}]},"resultCode": 0,"errMsg": "" }# 獲取json_data2中reportList數組的第2個值 print(jsonpath.jsonpath(json_data2, '$.body.reportList[1]')) [{'skuId': 13162, 'spuId': 20134, 'h5Url': 'https://m.2boss.cn/trade/3051443826783682574415?skuIdAlias=3051495335856701455454', 'orderTime': 1592980444000, 'cityId': 605, 'spuType': 1, 'reportUrl': 'https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592900443037', 'reportTitle': '上海漲漲漲小區榜', 'skuFullName': '上海漲漲漲小區榜', 'categoryName': '爆款報告', 'reportContent': '【最新6月榜】各區最有可能上漲小區,全新數據,幫你選最能升值的小區,各區房東溢價系數排行榜。', 'spuName': '上海漲漲漲小區榜', 'skuPrice': 1200}]# 獲取json_data2中reportList數組的所有值 print(jsonpath.jsonpath(json_data2, '$..reportList[0:]')) [{'spuName': '上海不夠賣小區榜', 'reportContent': '【最新6月榜】各區最火熱的小區,全新數據,那些才是真正最火的小區?各區庫存去化周期排行榜。', 'reportUrl': 'https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592899866409', 'cityId': 605, 'skuPrice': 1100, 'spuType': 1, 'h5Url': 'https://m.2boss.cn/trade/3051420769646018571885?skuIdAlias=3051483295561809939455', 'skuId': 13158, 'reportTitle': '上海不夠賣小區榜', 'categoryName': '爆款報告', 'spuId': 20130, 'orderTime': 1593507849000, 'skuFullName': '上海不夠賣小區榜'}, {'spuName': '上海漲漲漲小區榜', 'reportContent': '【最新6月榜】各區最有可能上漲小區,全新數據,幫你選最能升值的小區,各區房東溢價系數排行榜。', 'reportUrl': 'https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592900443037', 'cityId': 605, 'skuPrice': 1200, 'spuType': 1, 'h5Url': 'https://m.2boss.cn/trade/3051443826783682574415?skuIdAlias=3051495335856701455454', 'skuId': 13162, 'reportTitle': '上海漲漲漲小區榜', 'categoryName': '爆款報告', 'spuId': 20134, 'orderTime': 1592980444000, 'skuFullName': '上海漲漲漲小區榜'}, {'name': '橙子', 'id': 111}]# 獲取json_data2中reportList數組中包含key為name的所有字段 print(jsonpath.jsonpath(json_data2, '$..reportList[?(@.name)]')) [{'id': 111, 'name': '橙子'}]# 獲取json_data2中reportList數組中skuPrice>1100的所有值 print(jsonpath.jsonpath(json_data2, '$..reportList[?(@.skuPrice>1100)]')) [{'reportContent': '【最新6月榜】各區最有可能上漲小區,全新數據,幫你選最能升值的小區,各區房東溢價系數排行榜。', 'reportUrl': 'https://2boss.oss-cn-hangzhou.aliyuncs.com/dr2_banner/banner_1592900443037', 'categoryName': '爆款報告', 'cityId': 605, 'reportTitle': '上海漲漲漲小區榜', 'skuPrice': 1200, 'spuId': 20134, 'spuName': '上海漲漲漲小區榜', 'h5Url': 'https://m.2boss.cn/trade/3051443826783682574415?skuIdAlias=3051495335856701455454', 'spuType': 1, 'skuFullName': '上海漲漲漲小區榜', 'orderTime': 1592980444000, 'skuId': 13162}]

總結

以上是生活随笔為你收集整理的Python深层解析json数据之JsonPath的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。