如何遍历JSON中的条目? [英] How can I loop over entries in JSON?

查看:52
本文介绍了如何遍历JSON中的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历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']

I want to loop over the content of a JSON file and print it to the console.

I think I did mix up something with lists.

This is what I tried to get all the team_name elements

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:)

{
    "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)

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?

解决方案

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.

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']

这篇关于如何遍历JSON中的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆