请教,py取嵌套字典里的数值
有如下字典(jison){"result":{"face_num":1,
"face_list":[{"face_location":{"top":93,"left":275,"width":191,"height":273},"gender":"Male","age":"32"}]
},
"code":"0","message":"success"}
我想取其中的top left width height 数值,应该如何取到,谢谢 sd={"result":
{"face_num":1,
"face_list":[{"face_location":{"top":93,"left":275,"width":191,"height":273},"gender":"Male","age":"32"}]
},
"code":"0","message":"success"}
qz=sd['result']['face_list']['face_location']
for i in qz:
print(qz) 挨着找下一级就行了,取到最后的字典,循环取到键,利用键取值 JSON.result.face_list.face_location.top
JSON.result.face_list.face_location.left
JSON.result.face_list.face_location.width
JSON.result.face_list.face_location.height 《Python编程:入门到实践》 s = {"result":
{"face_num": 1,
"face_list": [
{"face_location": {"top": 93, "left": 275, "width": 191, "height": 273}, "gender": "Male", "age": "32"}]
},
"code": "0", "message": "success"}
print(s["result"]["face_list"]["face_location"]["top"])
print(s["result"]["face_list"]["face_location"]["left"])
print(s["result"]["face_list"]["face_location"]["width"])
print(s["result"]["face_list"]["face_location"]["height"]) 对key取就可以,类似于多维矩阵切片,不过这里矩阵的下标被换成了key ```python
import json
a = '''{"result":
{"face_num":1,
"face_list":[{"face_location":{"top":93,"left":275,"width":191,"height":273},"gender":"Male","age":"32"}]
},
"code":"0","message":"success"}'''
s = json.loads(a)
s['result']['face_list']['face_location']['top']
```
页:
[1]