Tôi đang cố gắng lấy mã zip cho một thành phố cụ thể bằng cách sử dụng zippopotam.us. Tôi có đoạn code sau đây mà làm việc, trừ khi tôi cố gắng truy cập vào phím post code
trả về TypeError: expected string or buffer
Python Truy cập dữ liệu JSON lồng nhau
r = requests.get('http://api.zippopotam.us/us/ma/belmont')
j = r.json()
data = json.loads(j)
print j['state']
print data['places']['latitude']
sản lượng Full 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"
}
Nhờ sự giúp đỡ của bạn.