随心一记

一二三四五,上山打老鼠


  • 首页

  • 归档

  • 标签
ywcsb

ywcsb

游戏可以不玩,小说不能不看。

153 日志
3 分类
42 标签
RSS
GitHub 知乎 随心一记
Links
  • 随心一记
  • 追梦人物的
  • MSDN

Python JsonPath模块使用

发表于 2023-02-17 | 阅读 1504 | 分类于 Python |

JsonPath

JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。适用于进行数据交互的场景,比如网站前台与后台之间的数据交互。

json示例数据

{
    "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
        }
    }
}

如果有一个多层嵌套的复杂JSON,想要根据key-value 或下标 的方式去批量获取JSON数据里面的内容,是比较麻烦的。JsonPath模块就能很好的解决这个问题。

JsonPath 以一种简单的方法来提取给定的JSON的内容,JsonPath 支持多种编程语言,如JavaScript、Java、Python、和php。

JsonPath 语法规则

官方文档: https://goessner.net/articles/JsonPath/

JsonPath 提供的JSON解析功能非常强大,它提供了类似正则表达式的语法,基本上可以满足所有想要获得的JSON内容。

规则:

JsonPath 描述
$ 根节点
@ 现行节点
.或[] 取子节点
* 匹配所有元素节点
[] 迭代器标示(可以在里面做简单的迭代操作,如数组下标,根据内容选值等)
[,] 支持迭代器中做多选。连接操作符在XPath 结果合并其它结点集合。Jsonpath允许name或者数组索引。
?() 支持过滤操作
[start\:end\:step] 数组分割操作从ES4借鉴。
() 脚本表达式,使用底层脚本引擎。支持表达式计算

简单使用:

JsonPath 描述
$.store.book[*].author 获取店内所有书籍的作者
$…author 获取所有作者
$.store.* 获取store的所有元素。所有的book和bicycle
$.store…price 获取store里面所有东西的价格
$…book[2] 获取第三本书的所有信息
$..book[(@.length-1)]或$..book[-1:] 获取最后一本书的所有信息
$..book[0,1]或 $..book[:2] 获取前面两本书的所有信息
$…book[?(@.isbn)] 过滤出所有的包含isbn的书信息
$…book[?(@.price<10)] 过滤出价格低于10的书
$…* 获取所有元素

Python中JsonPath使用

安装

pip install jsonpath

以Json数据模拟接口返回情况,通过jsonpath模块获取需要的数据

from jsonpath import jsonpath

res = {
    "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
        }
    }
}

# 过滤出所有的包含isbn的书信息
data_book = jsonpath(res,'$..book[?(@.isbn)]')
print(data_book)

# 获取店内所有书籍的作者
data_author = jsonpath(res,'$.store.book[*].author')
print(data_author)

结果

[{'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}]

['Nigel Rees', 'Evelyn Waugh', 'Herman Melville', 'J. R. R. Tolkien']
觉得不错,支持一下!
geerniya WeChat Pay

微信打赏

geerniya Alipay

支付宝打赏

# Python
celery rabbitmq 延迟消息
ingress 中添加proxy-connect-timeout 超时时间不生效

发表评论

共 0 条评论

    暂无评论
© 2018 - 2022 ywcsb
冀ICP备17022045号-1
Supported by 腾讯云