weeee4 发表于 2020-11-23 12:43

请教,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 数值,应该如何取到,谢谢

yzqhj 发表于 2020-11-23 12:50

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)

yzqhj 发表于 2020-11-23 12:53

挨着找下一级就行了,取到最后的字典,循环取到键,利用键取值

ghd19940802 发表于 2020-11-23 13:48

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

pixuan 发表于 2020-11-23 14:12

《Python编程:入门到实践》

kang_alone 发表于 2020-11-23 14:16

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"])

hhhhpaa 发表于 2020-11-23 14:38

对key取就可以,类似于多维矩阵切片,不过这里矩阵的下标被换成了key

52loli 发表于 2020-11-23 14:38

```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]
查看完整版本: 请教,py取嵌套字典里的数值