我正在开发一个连接到数据库并返回一个条目的 python 程序。我收到以下响应,但无法从结果中提取“地理围栏”的变量。
我可以通过response["Items"]
获得“元素” ,但我不知道如何进入“地理围栏”,如:"S": "Geofence"
{
"Count": 1,
"Items": [
{
"Lat": {
"N": "34.065"
},
"Serial": {
"S": "0001"
},
"Lon": {
"N": "32.875"
},
"Geofence": {
"S": "Geofence"
},
"Time": {
"S": "20170221T010628Z"
}
}
],
"LastEvaluatedKey": {
"Serial": {
"S": "0001"
},
"Time": {
"S": "20170221T010628Z"
}
},
"ScannedCount": 1
}
}
最佳答案
由于 response["Items"]
是一个列表,因此您需要相关索引(在本例中为 0),然后导航到键 "Geofence"
并“S”
print (response["Items"][0]["Geofence"]["S"])
将输出结果:
"Geofence"
如果列表中有多个项目,您可以循环它们:
res = []
for item in di["Items"]:
res.append(item["Geofence"]["S"])
print (res)
>>> ['Geofence']
关于Python 响应数据类型 - 如何访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42395654/