今天发现一个提取json数据比较方便的模块jsonpath,它可以像xpath一样提取数据。
jsonpath语法:
测试数据如图:
我现在要提取card:434345432的数据,只需要这样:
如图:
提取info下面的所有数据:
如图:
测试代码如下:
from jsonpath import jsonpath d = { "error_code": 0, "stu_info": [ { "id": 2059, "name": "小白", "sex": "男", "age": 28, "addr": "河南省济源市北海大道32号", "grade": "天蝎座", "phone": "18378309272", "gold": 10896, "info": { "card": 434345432, "bank_name": '中国银行' } }, { "id": 2067, "name": "小黑", "sex": "男", "age": 28, "addr": "河南省济源市北海大道32号", "grade": "天蝎座", "phone": "12345678915", "gold": 100 } ] } res1 = jsonpath(d, "$...card") print(res1) res = jsonpath(d, "$..info.*") print(res)
参考:https://www.cnblogs.com/denise1108/p/10265911.html