1 import jsonpath 2 3 d = { 4 "error_code": 0, 5 "all_stu": [ 6 { 7 "id": 1, 8 "name": "小黑", 9 "sex": "男", 10 "age": 18 , 11 "addr": "北京市昌平区回龙观", 12 "phone": "18612532945", 13 "grade":"巨蟹座", 14 "gold":100 15 }, 16 { 17 "id": 2, 18 "name": "小白", 19 "sex": "男", 20 "age": 18 , 21 "addr": "北京市昌平区回龙观", 22 "phone": "18612532945", 23 "grade":"巨蟹座", 24 "gold":100 25 } 26 ] 27 } 28 29 # 获取第一个stu的name:直接取,需一层一层的取 30 print(d.get("all_stu")[0].get("name")) 31 32 33 # 通过jsonpath取 34 result = jsonpath.jsonpath(d,‘$.all_stu[0].name‘) 35 print(result) 36 37 # 取所有的name 38 result1 = jsonpath.jsonpath(d,‘$..name‘) 39 print(result1)
原文:https://www.cnblogs.com/tour8/p/13122154.html