打印 JSON 个元素
Printing JSON elements
关于如何打印 JSON 元素,我有些不明白。使用 Yelp,感谢这个查询“https://api.yelp.com/v3/businesses/search?cc=FR&location=Toulouse&categories=movietheaters&limit=1”,我可以检索这个 JSON:
{
"businesses": [
{
"id": "gaumont-wilson-toulouse-2",
"name": "Gaumont Wilson",
"image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/dYJc874NnEJ9-jX2amrLvw/o.jpg",
"is_closed": false,
"url": "https://www.yelp.com/biz/gaumont-wilson-toulouse-2?adjust_creative=Xi9rQmCT871UpMvNRzAfuw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=Xi9rQmCT871UpMvNRzAfuw",
"review_count": 165,
"categories": [
{
"alias": "movietheaters",
"title": "Cinema"
}
],
"rating": 4,
"coordinates": {
"latitude": 43.6044154,
"longitude": 1.4475916
},
"transactions": [],
"location": {
"address1": "3 place du Président Thomas Wilson",
"address2": null,
"address3": null,
"city": "Toulouse",
"zip_code": "31000",
"country": "FR",
"state": "31",
"display_address": [
"3 place du Président Thomas Wilson",
"31000 Toulouse",
"France"
]
},
"phone": "+33534445050",
"display_phone": "+33 5 34 44 50 50",
"distance": 451.43923036020004
}
],
"total": 11,
"region": {
"center": {
"latitude": 43.602510035320684,
"longitude": 1.4426422119140625
}
}
}
然后我这样研究JSON:
response_data = response.json()
for i in response_data['businesses']:
print i['name']
但是 'name'
是我唯一可以打印的东西!
我无法打印 'address1'
、'city'
或 'zip_code'
。
为什么?
因为 JSON 中的那个层级中不存在这些键。它们存在于 "location"
键对应的字典中。你想使用:
print i["name"]["location"]["address1"]
因为那些存在于 "location" 子词典中。你需要这样的东西:
print i["name"]["location"]["address1"]
访问数据。
答案是:
for element in response_data['businesses']:
id = element['id']
name = element['name']
city = element['location']['city']
zip_code = element['location']['zip_code']
state = element['location']['state']
display_address = element['location']['display_address']
latitude = element['coordinates']['latitude']
longitude = element['coordinates']['longitude']
phone = element['phone']
print id, name, city, zip_code, state, display_address, latitude, longitude, phone
关于如何打印 JSON 元素,我有些不明白。使用 Yelp,感谢这个查询“https://api.yelp.com/v3/businesses/search?cc=FR&location=Toulouse&categories=movietheaters&limit=1”,我可以检索这个 JSON:
{
"businesses": [
{
"id": "gaumont-wilson-toulouse-2",
"name": "Gaumont Wilson",
"image_url": "https://s3-media1.fl.yelpcdn.com/bphoto/dYJc874NnEJ9-jX2amrLvw/o.jpg",
"is_closed": false,
"url": "https://www.yelp.com/biz/gaumont-wilson-toulouse-2?adjust_creative=Xi9rQmCT871UpMvNRzAfuw&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=Xi9rQmCT871UpMvNRzAfuw",
"review_count": 165,
"categories": [
{
"alias": "movietheaters",
"title": "Cinema"
}
],
"rating": 4,
"coordinates": {
"latitude": 43.6044154,
"longitude": 1.4475916
},
"transactions": [],
"location": {
"address1": "3 place du Président Thomas Wilson",
"address2": null,
"address3": null,
"city": "Toulouse",
"zip_code": "31000",
"country": "FR",
"state": "31",
"display_address": [
"3 place du Président Thomas Wilson",
"31000 Toulouse",
"France"
]
},
"phone": "+33534445050",
"display_phone": "+33 5 34 44 50 50",
"distance": 451.43923036020004
}
],
"total": 11,
"region": {
"center": {
"latitude": 43.602510035320684,
"longitude": 1.4426422119140625
}
}
}
然后我这样研究JSON:
response_data = response.json()
for i in response_data['businesses']:
print i['name']
但是 'name'
是我唯一可以打印的东西!
我无法打印 'address1'
、'city'
或 'zip_code'
。
为什么?
因为 JSON 中的那个层级中不存在这些键。它们存在于 "location"
键对应的字典中。你想使用:
print i["name"]["location"]["address1"]
因为那些存在于 "location" 子词典中。你需要这样的东西:
print i["name"]["location"]["address1"]
访问数据。
答案是:
for element in response_data['businesses']:
id = element['id']
name = element['name']
city = element['location']['city']
zip_code = element['location']['zip_code']
state = element['location']['state']
display_address = element['location']['display_address']
latitude = element['coordinates']['latitude']
longitude = element['coordinates']['longitude']
phone = element['phone']
print id, name, city, zip_code, state, display_address, latitude, longitude, phone