[英]How to loop through complex JSON in Python 3.x
目前,我正在为我的家构建一个仪表板,从 Honeywell API 获取温度值。 我正在尝试遍历从 API 获得的复杂 JSON 以获取所有设备当前温度。
由于我对 Python 很陌生,我不知道如何进行排序。 这是一个示例响应:
[
{
"locationID": 5018081,
"type": "Residential",
"hasStation": true,
"devices": [
{
"gatewayId": 4773548,
"deviceID": 6575395,
"thermostatModelType": "EMEA_ZONE",
"deviceType": 128,
"name": "Badkamer 1e",
"scheduleCapable": false,
"holdUntilCapable": false,
"thermostat": {
"units": "Celsius",
"indoorTemperature": 18.8300,
"outdoorTemperature": 128.0000,
"outdoorTemperatureAvailable": false,
"outdoorHumidity": 128.0000,
"outdootHumidityAvailable": false,
"indoorHumidity": 128.0000,
"indoorTemperatureStatus": "Measured",
"indoorHumidityStatus": "NotAvailable",
"outdoorTemperatureStatus": "NotAvailable",
"outdoorHumidityStatus": "NotAvailable",
"isCommercial": false,
"allowedModes": [
"Heat",
"Off"
],
"deadband": 0.0000,
"minHeatSetpoint": 5.0000,
"maxHeatSetpoint": 35.0000,
"minCoolSetpoint": 50.0000,
"maxCoolSetpoint": 90.0000,
"changeableValues": {
"mode": "Off",
"heatSetpoint": {
"value": 19.0,
"status": "Scheduled"
},
"vacationHoldDays": 0
},
"scheduleCapable": false,
"vacationHoldChangeable": false,
"vacationHoldCancelable": false,
"scheduleHeatSp": 0.0000,
"scheduleCoolSp": 0.0000
},
"alertSettings": {
"deviceID": 6575395,
"tempHigherThanActive": true,
"tempHigherThan": 30.0000,
"tempHigherThanMinutes": 0,
"tempLowerThanActive": true,
"tempLowerThan": 5.0000,
"tempLowerThanMinutes": 0,
"faultConditionExistsActive": false,
"faultConditionExistsHours": 0,
"normalConditionsActive": true,
"communicationLostActive": false,
"communicationLostHours": 0,
"communicationFailureActive": true,
"communicationFailureMinutes": 15,
"deviceLostActive": false,
"deviceLostHours": 0
},
"isUpgrading": false,
"isAlive": true,
"thermostatVersion": "02.00.17.00",
"macID": "B82CA0CCEB90",
"locationID": 5018081,
"domainID": 60304,
"instance": 9
},
{
"gatewayId": 4773548,
"deviceID": 6575388,
"thermostatModelType": "EMEA_ZONE",
"deviceType": 128,
"name": "Badkamer BG",
"scheduleCapable": false,
"holdUntilCapable": false,
"thermostat": {
"units": "Celsius",
"indoorTemperature": 20.3200,
"outdoorTemperature": 128.0000,
"outdoorTemperatureAvailable": false,
"outdoorHumidity": 128.0000,
"outdootHumidityAvailable": false,
"indoorHumidity": 128.0000,
"indoorTemperatureStatus": "Measured",
"indoorHumidityStatus": "NotAvailable",
"outdoorTemperatureStatus": "NotAvailable",
"outdoorHumidityStatus": "NotAvailable",
"isCommercial": false,
"allowedModes": [
"Heat",
"Off"
],
"deadband": 0.0000,
"minHeatSetpoint": 5.0000,
"maxHeatSetpoint": 35.0000,
"minCoolSetpoint": 50.0000,
"maxCoolSetpoint": 90.0000,
"changeableValues": {
"mode": "Off",
"heatSetpoint": {
"value": 19.0,
"status": "Scheduled"
},
"vacationHoldDays": 0
},
"scheduleCapable": false,
"vacationHoldChangeable": false,
"vacationHoldCancelable": false,
"scheduleHeatSp": 0.0000,
"scheduleCoolSp": 0.0000
},
"alertSettings": {
"deviceID": 6575388,
"tempHigherThanActive": true,
"tempHigherThan": 30.0000,
"tempHigherThanMinutes": 0,
"tempLowerThanActive": true,
"tempLowerThan": 5.0000,
"tempLowerThanMinutes": 0,
"faultConditionExistsActive": false,
"faultConditionExistsHours": 0,
"normalConditionsActive": true,
"communicationLostActive": false,
"communicationLostHours": 0,
"communicationFailureActive": true,
"communicationFailureMinutes": 15,
"deviceLostActive": false,
"deviceLostHours": 0
},
"isUpgrading": false,
"isAlive": true,
"thermostatVersion": "02.00.17.00",
"macID": "B82CA0CCEB90",
"locationID": 5018081,
"domainID": 60304,
"instance": 2
},
{
"gatewayId": 4773548,
"deviceID": 6575389,
"thermostatModelType": "EMEA_ZONE",
"deviceType": 128,
"name": "Garage",
"scheduleCapable": false,
"holdUntilCapable": false,
"thermostat": {
"units": "Celsius",
"indoorTemperature": 17.1600,
"outdoorTemperature": 128.0000,
"outdoorTemperatureAvailable": false,
"outdoorHumidity": 128.0000,
"outdootHumidityAvailable": false,
"indoorHumidity": 128.0000,
"indoorTemperatureStatus": "Measured",
"indoorHumidityStatus": "NotAvailable",
"outdoorTemperatureStatus": "NotAvailable",
"outdoorHumidityStatus": "NotAvailable",
"isCommercial": false,
"allowedModes": [
"Heat",
"Off"
],
"deadband": 0.0000,
"minHeatSetpoint": 5.0000,
"maxHeatSetpoint": 35.0000,
"minCoolSetpoint": 50.0000,
"maxCoolSetpoint": 90.0000,
"changeableValues": {
"mode": "Off",
"heatSetpoint": {
"value": 17.0,
"status": "Scheduled"
},
"vacationHoldDays": 0
},
"scheduleCapable": false,
"vacationHoldChangeable": false,
"vacationHoldCancelable": false,
"scheduleHeatSp": 0.0000,
"scheduleCoolSp": 0.0000
},
"alertSettings": {
"deviceID": 6575389,
"tempHigherThanActive": true,
"tempHigherThan": 30.0000,
"tempHigherThanMinutes": 0,
"tempLowerThanActive": true,
"tempLowerThan": 5.0000,
"tempLowerThanMinutes": 0,
"faultConditionExistsActive": false,
"faultConditionExistsHours": 0,
"normalConditionsActive": true,
"communicationLostActive": false,
"communicationLostHours": 0,
"communicationFailureActive": true,
"communicationFailureMinutes": 15,
"deviceLostActive": false,
"deviceLostHours": 0
},
"isUpgrading": false,
"isAlive": true,
"thermostatVersion": "02.00.17.00",
"macID": "B82CA0CCEB90",
"locationID": 5018081,
"domainID": 60304,
"instance": 3
}
],
"oneTouchButtons": [],
"weather": {
"condition": "Cloudy",
"temperature": 8.7,
"units": "Celsius",
"humidity": 91,
"phrase": "Cloudy"
},
"daylightSavingTimeEnabled": true,
"timeZone": {
"id": "W. Europe Standard Time",
"displayName": "(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna",
"offsetMinutes": 60,
"currentOffsetMinutes": 60,
"usingDaylightSavingTime": true
},
"oneTouchActionsSuspended": false,
"isLocationOwner": true,
"locationOwnerID": 4172939,
"canSearchForContractors": true
}
]
这里有谁知道如何获取设备数组中的每个设备thermostat.indoortemperature
和thermostat.changeableValues.HeatSetpoint.Value
?
看起来你有一个包含一本大字典的列表。 该字典有一个devices
键值对,其中值是一个字典列表,每个设备一个。 如果data
是您的 JSON 结构,您可以通过以下方式遍历设备字典:
data = ...
for device in data[0]["devices"]:
indoor_temp = device["thermostat"]["indoorTemperature"]
heat_set_point = device["changeableValues"]["heatSetpoint"]["value"]
print("Device ID: {}".format(device["deviceID"]))
print("Indoor temperature: {}".format(indoor_temp))
print("Heat set point: {}\n".format(heat_set_point))
这是一个 json 数组.. 将其转换为字典数组,然后循环遍历它并使用其对应的键从每个字典中获取值。
import json
json_array_string = u'[{ "key":"value", ... }, {...}, ... ]'
array = json.loads(json_string)
正如评论中提到的,这是一个包含字典元素的列表。
保存列表,我另存a
(确保真/假为真/假)
对于每个位置,该列表的长度为 1。
选择列表的第一个元素
a[0]
该列表包含一个字典,因此请打开字典以获取所需的值devices
。 导航元素就像在您的计算机上导航文件结构
a[0]['devices']
这为您提供了一个包含三个字典的列表
遍历字典。 参考thermostat
子字典并选择indoorTemperature
键
[i['thermostat']['indoorTemperature'] for i in a[0]['devices']]
这为您提供了每个设备的“室内温度”
我不知道你想要什么 output,但你可以使用这个逻辑来得到你需要的东西!
一般来说,获取列表的值表示索引(例如a[0]
),获取字典的值表示键(例如 device['thermostat'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.