【发布时间】:2018-06-01 13:52:30
【问题描述】:
我正在尝试从 Google Places API 解析 JSON 文件。我正在尝试收集附近地址、lng/lat 坐标以及 Place ID。在包含 JSON 文件的页面上,我的代码正在捕获更多位置的信息,但由于某种原因,它没有从其中一个位置获取数据。我的代码是:
response = urllib.urlopen(url)
data = json.loads(response.read())
for n in range(len(data['results'])):
location = []
address = data['results'][n]['vicinity']
address = address[: address.find(", ")]
coords_lat = str(data['results'][n]['geometry']['location']['lat'])
coords_lat = coords_lat[:-4]
coords_lng = str(data['results'][n]['geometry']['location']['lng'])
coords_lng = coords_lng[:-4]
place_id = data['results'][n]['place_id']
location = [address, coords_lat, coords_lng, place_id]
print location
但是对于其中一个位置,我的程序正在返回:
...[u'C', '33.87', '-84.53', u'ChIJOUmV_jgX9YgRdN1sudI8yOI']...,而不是该位置的完整附近地址,'C, 2976 Ask-Kay Dr, Smyrna'。
This is the API JSON page with one of the results I'm referring to
【问题讨论】:
-
请附上文字。链接失效了,该网站的意义不仅在于帮助您,还在于帮助将来遇到同样问题的人。
-
嗨,Jared,我不太清楚你的意思。我包括了我的代码文本和错误输出的文本。你还建议我包括什么?
-
请在 JSON 中进行编辑,而不是让它被外部链接。尽管就其价值而言,这个问题在质量方面很容易在该网站上的第一篇文章中排名前四分之一:)
-
我只是不想在实际问题中添加太多不必要的文字。我不想阻止任何可以帮助阅读它的人,因为它太长了。我会记住这一点,以便我提出问题。另外,感谢您的支持! (:
-
对于 SO 问题,您希望包含重现问题所必需的所有内容仅此而已。重现的最小测试用例通常包括一些样本数据以及当前输出和所需输出,例如
my_function({"foo": 2}) prints {"foo": 4} but should be {"foo": 8}以及my_function的代码。没问题:)
标签: python json parsing google-places-api