我正在研究连接到数据库并返回条目的python程序。我得到以下响应,但是我无法从结果中提取“ Geofence”的变量。
我可以通过执行response["Items"]
来获得“物品”,但是我无法弄清楚如何使用“ Geofence”,如:"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']