python中jsonpath的用法

    接口返回的json数据,需要取值后断言,一般我们是使用jsonpath来提取接口返回的数据 JsonPath模块,是一个专门用于处理Json字符串的模块。JsonPath相当于是Xpath 部署JsonPath 通过pip install jsonpath来进行安装 通过JsonPath获得的内容,会以list的形式进行返回,也就意味着你的jsonpath是可以 有一个值或者多个值同时存在的。 如果要基于JsonPath来处理json数据,就一定要去同步处理list JsonPath定义中,如果表达式出现错误,则会返回False(布尔类型的值) JsonPath要么返回False,要么返回list。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import jsonpath,json

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
        }
    },
    "expensive": 10
}

# 基于JsonPath获取元素:通过jsonpath函数来获取(json数据,定位表达式)
"""
    jsonpath表达式的基本格式规范:
        $ 表示根节点,也是所有jsonpath表达式的开始
        . 表示获取子节点
        .. 表示获取所有符合条件的内容
        *  代表所有的元素节点
        [] 表示迭代器的标示(可以用于处理下标等情况)
        [,] 表示多个结果的选择
        ?() 表示过滤操作
        @ 表示当前节点
"""
bike = jsonpath.jsonpath(data,'$.store.bicycle.color')
print(bike)
print(bike[0])
book = jsonpath.jsonpath(data,'$.store.book')
print(book)
# 获取store下所有的price节点的值
price = jsonpath.jsonpath(data,'$.store..price')
print(price)
# 连接操作符,在xpath中 结果合并其他结果的集合。JSONPATH允许name或者数据索引。类似于xpath |
book_price = jsonpath.jsonpath(data,'$.store.book[0,1,2].price')
#切片遵循list的切片规则
book_price1 = jsonpath.jsonpath(data,'$.store.book[0:3].price')
print(book_price)
print(f'book_price1:{book_price1}')
# 获取price大于10的所有book
book_1 = jsonpath.jsonpath(data,'$..book[?(@.price>10)]')
print(book_1)
# 这是一条错误的JsonPath
book_2 = jsonpath.jsonpath(data,'$..展昭[?(@.price>10)]')
print(book_2)
print(type(book_2))
print(type(data))

结果集:

['red']
red
[[{'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}]]
[8.95, 12.99, 8.99, 22.99, 19.95]
[8.95, 12.99, 8.99]
book_price1:[8.95, 12.99, 8.99]
[{'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}]
False
<class 'bool'>
<class 'dict'>

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年7月15日
下一篇 2023年7月15日

相关推荐