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

这是我的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)

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

以下是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?

推荐答案

实际上,要查询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']

Json相关问答推荐

如何在PowerShell中访问嵌套的JSON字段

ArcGIS json到Geojson的变换

Jolt变换将字段移动到数组的每个元素中

在Vega中如何通过滑块改变条形图的宽度

Oracle plsql:如何将json文件加载到嵌套表中

jq:将一个数组与多个数组连接起来

使用 JOLT 将日期格式转换为 JSON

导致此 Kotlin Retrofit2 错误的可能原因有哪些?

将不带正文的 CURL POST 转换为 RESTRequest Delphi 代码 / 为 Dropbox API /get_current_account 端点编写 Delphi 代码

使用Powershell将JSON文件加载到SQL Server表格

匹配来自不同数组的值

小写嵌套特定键的震动转换

如何在 Flutter 中遍历嵌套的动态 JSON 文件

Jackson:忽略 Json 配置值

强制 JSON.NET 在序列化 DateTime 时包含毫秒(即使 ms 组件为零)

python追加到json对象中的数组

如何从 MVC 控制器返回 Json 对象以查看

JSON - 是否有任何 XML CDATA 类似功能?

为什么 RestTemplate 不将响应表示绑定到 PagedResources?

如何遍历 JSON 中的条目?