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'>

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值