Python 如何遍历 JSON 中的条目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14547916/
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
How can I loop over entries in JSON?
提问by mcbetz
I want to loop over the content of a JSON file and print it to the console.
我想遍历 JSON 文件的内容并将其打印到控制台。
I think I did mix up something with lists.
我想我确实把一些东西和清单混在一起了。
This is what I tried to get all the team_nameelements
这就是我试图获得所有team_name元素的方法
from urllib2 import urlopen
import json
url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1'
response = urlopen(url)
json_obj = json.load(response)
for i in json_obj['team']:
print i
And this is my JSON (simplified:)
这是我的 JSON(简化:)
{
"team": [
{
"team_icon_url": "http://www.openligadb.de/images/teamicons/Hamburger_SV.gif",
"team_id": "100",
"team_name": "Hamburger SV"
},
{
"team_icon_url": "http://www.openligadb.de/images/teamicons/FC_Schalke_04.gif",
"team_id": "9",
"team_name": "FC Schalke 04"
}
]
}
(Full JSON output to be found here: Link)
(完整的 JSON 输出可在此处找到:链接)
And of course I get an error, that I should use integer input in [], not string, but I don't get how I could do that.
当然,我得到了一个错误,我应该在 [] 中使用整数输入,而不是字符串,但我不知道如何做到这一点。
for i in json_obj['team']:
TypeError: string indices must be integers, not str
Here is the response:
这是response:
http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1
<addinfourl at 139755086292608 whose fp = <socket._fileobject object at 0x7f1b446d33d0>>
What did I get wrong?
我做错了什么?
采纳答案by mcbetz
Actually, to query the team_name, just add it in brackets to the last line. Apart from that, it seems to work on Python 2.7.3 on command line.
实际上,要查询team_name,只需将其添加到最后一行的括号中即可。除此之外,它似乎在命令行上适用于 Python 2.7.3。
from urllib2 import urlopen
import json
url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1'
response = urlopen(url)
json_obj = json.load(response)
for i in json_obj['team']:
print i['team_name']
回答by Prem Minister
Try this :
尝试这个 :
import urllib, urllib2, json
url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1'
request = urllib2.Request(url)
request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
request.add_header('Content-Type','application/json')
response = urllib2.urlopen(request)
json_object = json.load(response)
#print json_object['results']
if json_object['team'] == []:
print 'No Data!'
else:
for rows in json_object['team']:
print 'Team ID:' + rows['team_id']
print 'Team Name:' + rows['team_name']
print 'Team URL:' + rows['team_icon_url']
回答by Karoly Horvath
To decode json, you have to pass the json string. Currently you're trying to pass an object:
要解码 json,您必须传递 json 字符串。目前您正在尝试传递一个对象:
>>> response = urlopen(url)
>>> response
<addinfourl at 2146100812 whose fp = <socket._fileobject object at 0x7fe8cc2c>>
You can fetch the data with response.read().
您可以使用 获取数据response.read()。

