编程语言
首页 > 编程语言> > Python之jsonpath模块

Python之jsonpath模块

作者:互联网

Python之jsonpath模块

import jsonpath

d = {
    "error_code": 0,
    "stu_info": [
        {
            "id": 314,
            "name": "矿泉水",
            "sex": "男",
            "age": 18,
            "addr": "北京市昌平区",
            "grade": "摩羯座",
            "phone": "18317155663",
            "gold": 100,
            "cars": [
                {"car1": "bmw"},
                {"car2": "ben-z"},
            ]
        }
    ]

}
result=jsonpath.jsonpath(d,'$..car2') #模糊匹配
result=jsonpath.jsonpath(d,'$.stu_info') #取到stu_info这里的所有内容
result = jsonpath.jsonpath(d, '$.stu_info[0]') #取到stu_info里的第1个元素
result = jsonpath.jsonpath(d, '$.stu_info[0].id')  # 取到stu_info里的第1个元素中的id
print(result)

标签:info,Python,取到,stu,jsonpath,result,模块,id
来源: https://blog.csdn.net/qq_42610167/article/details/120615194