python 解析Json对象之jsonpath_rw用法

jsonpath_rw:一个可以像写xpath一样写json的Python第三方库

首先安装:pip install jsonpath_rw

实例:

from jsonpath_rw import jsonpath,parse

data = {
        "error_code": 0,
        "stu_info": [
                {
                        "id": 309,
                        "name": "小白",
                        "sex": "男",
                        "age": 28,
                        "addr": "河南省济源市北海大道32号",
                        "grade": "天蝎座",
                        "phone": "18512572946",
                        "gold": 100
                },
                {
                        "id": 310,
                        "name": "小白",
                        "sex": "男",
                        "age": 28,
                        "addr": "河南省济源市北海大道32号",
                        "grade": "天蝎座",
                        "phone": "18516572946",
                        "gold": 100
                }
        ]
}

json_expr = parse('stu_info[0].name')
male = json_expr.find(data)
print([math.value for math in male])

官方文档地址: https://github.com/kennknowles/python-jsonpath-rw

你可能感兴趣的:(Python)