Python 响应数据类型 - 如何访问数据

标签 python python-2.7 data-structures

我正在开发一个连接到数据库并返回一个条目的 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/

相关文章:

python - 调用 PyRun_String(...) 返回 NULL 后如何获取异常信息?

python - Vim:高亮显示第 80 列的单个字符

python - ImportError - 将 Falcon 应用程序部署到 AWS Elastic Beanstalk

python - 通过socket发送wav文件

python - 基本 flask : ImportError: cannot import name Flask

python - ete2如果一个节点是一对坐标怎么操作

python - 两个列表中对应的float

c++ - 连接二叉树同一层的节点

data-structures - 关于完全二叉树的困惑

java - java整数集的内存有效存储