jsonpath安装_python-jsonpath 解析神器

JsonPath是一种用于提取JSON文档内容的工具,具有类似正则表达式的语法。本文介绍了如何在Python中安装JsonPath,提供了一些基本的JsonPath语法规则和使用示例,并推荐了几款实用的在线JSON工具。
摘要由CSDN通过智能技术生成

49bd6f447691c6a52dd4f35fab14bece.png

前言

jsonpath用来解析json数据使用的,是一种简单的方法来提取给定JSON文档的部分内容。JsonPath有许多编程语言,如Javascript,Python和PHP,Java。

JsonPath提供的json解析非常强大,它提供了类似正则表达式的语法,基本上可以满足所有你想要获得的json内容。下面我把官网介绍的每个表达式用代码实现,可以更直观的知道该怎么用它。

环境安装


jsonpath是第三方模块,想要额外安装

pip install jsonpath

jsonpath语法规则

ca825f4c6eaa70f82a40811e53434b06.png

jsonpath使用示例

"""
Create by dell on 2020/10/4
作者 :wencheng
微信公众:自动化测试 To share
"""
import jsonpath

jsonobj = {
    "code": 0,
    "msg": "success",
    "data": {
        "output": [
            {
                "id": "867899031136606",
                "prediction": 0.698744767973,
                "instances": []
            },
            {
                "id": "867899031133728",
                "prediction": 0.695468634754,
                "instances": []
            }
        ]
    }
}
msg = jsonpath.jsonpath(jsonobj, '$.msg')
print("msg:%s" % msg)
print("================================")

data = jsonpath.jsonpath(jsonobj, '$.data')
print("data:%s" % data)
print("================================")

output = jsonpath.jsonpath(jsonobj, '$..output')
print("output:%s" % output)
print("================================")
id = jsonpath.jsonpath(jsonobj, '$...id')
print("id:%s" % id)

执行结果:

C:python3.6python.exe E:/Gitwokspace/Daily_py/Fileprocessing/test/jsonpath11.py

msg:['success']

================================

data:[{'output': [{'id': '867899031136606', 'prediction': 0.698744767973, 'instances': []}, {'id': '867899031133728', 'prediction': 0.695468634754, 'instances': []}]}]

================================

output:[[{'id': '867899031136606', 'prediction': 0.698744767973, 'instances': []}, {'id': '867899031133728', 'prediction': 0.695468634754, 'instances': []}]]

================================

id:['867899031136606', '867899031133728']

Process finished with exit code 0

这里再为大家推荐几款比较实用的json在线工具供大家参考使用

在线JSON代码检验、检验、美化、格式化工具: http://tools.jb51.net/code/json

JSON在线格式化工具: http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具: http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具: http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:http://tools.jb51.net/code/json_yasuo_trans

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。更多文章关注小编公众号:自动化测试 To share

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值