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_name
elements
这就是我试图获取所有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
:
以下是回复:
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?
我弄错了什么?
3 个解决方案
#1
44
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']
#2
6
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']
#3
0
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()获取数据。
#1
44
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']
#2
6
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']
#3
0
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()获取数据。