我想使用zippopotam.us獲取特定城市的郵政編碼。我有以下的代碼工作,除非我嘗試訪問post code
鍵返回TypeError: expected string or buffer
Python訪問嵌套的JSON數據
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"
}
感謝您的幫助。