【问题标题】:How to obtain only specific values from a nested JSON response from a Google API?如何从 Google API 的嵌套 JSON 响应中仅获取特定值?
【发布时间】:2019-11-14 12:43:33
【问题描述】:

我正在为 QGIS 构建反向地理编码插件,并且只需要从 JSON 文件中读取特定字段(例如,仅邮政编码)。 JSON 是对 Google Geolocation API 调用的响应,格式如下:

"results" : [
  {
     "address_components" : [
        {
           "long_name" : "10",
           "short_name" : "10",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Λεωφόρος Δημοκρατίας",
           "short_name" : "Λεωφ. Δημοκρατίας",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Πεύκα",
           "short_name" : "Πεύκα",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Θεσσαλονίκη",
           "short_name" : "Θεσσαλονίκη",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Ελλάδα",
           "short_name" : "GR",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "570 10",
           "short_name" : "570 10",
           "types" : [ "postal_code" ]
        }
     ],

我正在使用 python 中的 json.loads(...) 方法解析 JSON 文件,但似乎 python 将其识别为列表。

更新:查看您的答案后,我找到了解决方案,谢谢大家!

for item in results[0]["address_components"]: 
    if item["types"] == ["route"]:
      a = item["long_name"]

results 是从“results”解析出来的列表,通过上面的代码访问。 a 捕获带有“路由”名称的字符串的值。

【问题讨论】:

  • 它是一个列表。您需要遍历它并仅选择那些您认为相关的条目。
  • 当然是列表。您的“结果”是带有“address_components”的对象列表,而“address_components”又是一个对象列表。
  • 当我尝试获取 ''address_components'' 列表中的第 5 个功能时,我得到一个 KeyError,有什么建议吗?

标签: python json api qgis google-geocoding-api


【解决方案1】:
p={"results" : [
  {
     "address_components" : [
        {
           "long_name" : "10",
           "short_name" : "10",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Λεωφόρος Δημοκρατίας",
           "short_name" : "Λεωφ. Δημοκρατίας",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Πεύκα",
           "short_name" : "Πεύκα",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Θεσσαλονίκη",
           "short_name" : "Θεσσαλονίκη",
           "types" : [ "administrative_area_level_3", "political" ]
        },
        {
           "long_name" : "Ελλάδα",
           "short_name" : "GR",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "570 10",
           "short_name" : "570 10",
           "types" : [ "postal_code" ]
        }
     ]
     }
]
}
for item in p["results"][0]["address_components"]:
  if item["long_name"]=="570 10":

    print(item["types"])

【讨论】:

  • 将结果转换为带有列表的字典..相应地循环遍历它
  • 除此之外,您还可以使用索引循环遍历列表
【解决方案2】:

您需要遍历results 这是一个列表。如果你想获得第 5 个元素,你可以这样做:

results[0]['address_components'][4]

如果您想从列表中获取元素,请使用[id](将id 替换为元素编号),如果您想从字典中获取值,请使用'key'(将key 替换为名称)

【讨论】:

  • 我尝试使用你提到的结构,但我得到一个“列表索引超出范围错误”(参考 [4])
  • 您能否使用您的代码创建 pastebin 链接 (pastebin.com),或者编辑您的问题以包含您的代码?比只有 json 响应更容易看到发生了什么。
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 2016-03-12
  • 2021-02-04
相关资源
最近更新 更多