【发布时间】:2014-06-11 23:12:18
【问题描述】:
我正在尝试使用 zippopotam.us 获取特定城市的邮政编码。我有以下代码有效,除非我尝试访问返回 TypeError: expected string or buffer 的 post code 键
r = requests.get('http://api.zippopotam.us/us/ma/belmont')
j = r.json()
data = json.loads(j)
print j['state']
print data['places']['latitude']
完整的 JSON 输出:
{
"country abbreviation": "US",
"places": [
{
"place name": "Belmont",
"longitude": "-71.4594",
"post code": "02178",
"latitude": "42.4464"
},
{
"place name": "Belmont",
"longitude": "-71.2044",
"post code": "02478",
"latitude": "42.4128"
}
],
"country": "United States",
"place name": "Belmont",
"state": "Massachusetts",
"state abbreviation": "MA"
}
【问题讨论】:
-
将您的 JSON 放入 the tool in this snippet,选中“仅括号”,然后单击您想要将其代码路径复制到剪贴板的节点。