我想循环 JSON 文件的内容并将其打印到控制台。
我想我确实把一些东西和列表混淆了。
这就是我试图得到的所有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
这是我的 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"
}
]
}
(完整的 JSON 输出可在此处找到:)
当然,我收到一个错误,我应该在 [] 中使用整数输入,而不是字符串,但我不知道如何做到这一点。
for i in json_obj['team']:
TypeError: string indices must be integers, not str
这里是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>>
我做错了什么?
实际上,要查询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']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)