【问题标题】:Python Response Datatype - How to access dataPython 响应数据类型 - 如何访问数据
【发布时间】:2017-02-22 15:26:45
【问题描述】:

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

【问题讨论】:

    标签: python python-2.7 data-structures


    【解决方案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']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2021-02-12
      相关资源
      最近更新 更多