在 Python 中循环遍历 JSON 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42445237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through a JSON array in Python
提问by Abdul
I have the following data taken from an API. I am trying to access the restaurant name using a Python script and have the script display it. Here are my files:
我有以下来自 API 的数据。我正在尝试使用 Python 脚本访问餐厅名称并让脚本显示它。这是我的文件:
test.py
测试文件
with open('data.json') as data_file:
data = json.load(data_file)
for restaurant in data:
print data ['restaurants'][0]['restaurant']['name']
my JSON file is as follows: (simplified)
我的JSON文件如下:(简化)
{
"results_found": 3296,
"results_start": 0,
"results_shown": 20,
"restaurants": [
{
"restaurant": {
"R": {
"res_id": 9101083
},
"id": "9101083",
"name": "My Meat Wagon",
"address": "Market Square, Smithfield, Dublin Dublin 7",
"locality": "Smithfield",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3489980000",
"longitude": "-6.2788120000",
"zipcode": "Dublin 7",
"events_url": "https://www.zomato.com/dublin/my-meat-wagon-smithfield/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 9101628
},
"id": "9101628",
"name": "Wowburger",
"url": "https://www.zomato.com/dublin/wowburger-temple-bar?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "The Workmans Club, 11 Wellington Quay, Temple Bar, Dublin Dublin 2",
"locality": "The Workmans Club",
"city": "Dublin",
"city_id": 91,
"latitude": "53.3452863158",
"longitude": "-6.2663815543",
"zipcode": "Dublin 2",
"country_id": 97,
"locality_verbose": "The Workmans Club, Dublin"
},
"switch_to_order_menu": 0,
"cuisines": "Burger",
"average_cost_for_two": 20,
"establishment_types": []
}
},
{
"restaurant": {
"R": {
"res_id": 16520426
},
"id": "16520426",
"name": "Brother Hubbard",
"locality_verbose": "North City, Dublin"
},
Currently, it displays the first restaurant name three times. I want it to loop through each restaurant
object and display the value for the key "name". Any help would be appreciated.
目前,它会显示 3 次第一个餐厅名称。我希望它遍历每个restaurant
对象并显示键“名称”的值。任何帮助,将不胜感激。
回答by Daniel
When restaurants
is your list, you have to iterate over this key:
什么时候restaurants
是你的列表,你必须遍历这个键:
for restaurant in data['restaurants']:
print restaurant['restaurant']['name']
回答by Nassim Ben
with open('data.json') as data_file:
data = json.load(data_file)
for restaurant in data['restaurant']:
print restaurant['restaurant']['name']
This way you will loop over the elements in the list of dictionaries inside your 'restaurants' field and output their names.
通过这种方式,您将遍历“餐厅”字段内字典列表中的元素并输出它们的名称。
You were really close, what you were doing before was looping over all the main fields in your json file and print the name of the first restaurant every time (data['restaurants'][0]
gives you the first restaurant in the list of restaurants... and you printed its name every time)
你真的很接近,你之前所做的是循环遍历 json 文件中的所有主要字段并每次打印第一家餐厅的名称(data['restaurants'][0]
给你餐厅列表中的第一家餐厅......然后你打印了它的名字每次)