python - Python API 和字典
问题描述
我试图只打印说两个“设备”,我尝试了多种方法但没有任何运气,我该怎么做?此外,我将如何做一些事情来打印单个设备或什至两者的“模型”。我已经发布了我的代码以及下面的回复。
<class 'dict'>
{'data': {'devices': [{'device': ’00:00:00:00:00:00:00:00’, 'model': 'H6159', 'deviceName': 'Closet', 'controllable': True, 'retrievable': True, 'supportCmds': ['turn', 'brightness', 'color', 'colorTem']}, {'device': '00:00:00:00:00:00:00:00', 'model': 'H6003', 'deviceName': 'Living Room', 'controllable': True, 'retrievable': True, 'supportCmds': ['turn', 'brightness', 'color', 'colorTem']}]}, 'message': 'Success', 'code': 200}
解决方案
如果我理解正确,您希望通过键访问 dict rs 的值。因此,您想使用 rs[key] 访问 dict,例如 rs['data']['devices'][0]['model'] 以获取列表中第一个的设备型号。
rs['data'] -> get the data value of rs which is another dict.
rs['data']['devices'] -> get the devices in data which is a list. So you can access its element by index for example 0.
rs['data']['devices'][0] -> get the first device in the devices list
rs['data']['devices'][0]['model'] -> get the value of first device model.
推荐阅读
- python - 数据框在一个“单元格”中获取所有数据
- testing - 使用自动构建构建工作流
- javascript - 使用 AJAX 在 iframe 中放置 videoID
- javascript - 在本机反应中隐藏动态生成的按钮
- c# - 有没有办法在 .NET 4 中使用 C# 6 或更高版本?
- json - AdaptiveCardsJson - 点击通话选项而不打开新 URL
- php - 使用存储库模式编写 REST API 的函数 update() 出现问题
- python - 使用类自变量作为默认类方法参数
- cmd - 根据文件类型扩展名更改文件名
- microsoft-cognitive - HoloLens 上的 onnx 1.2 模型(自定义视觉)