python提取json数据索引必须是整数_解析JSON python时字符串索引必须是整数

我是python新手,在分析JSON文件时遇到了这个错误:string indices must be integers。

JSON文件:{"NFLTeams": [

{"code":"ARI","fullName":"Arizona Cardinals","shortName":"Arizona"},

{"code":"ATL","fullName":"Atlanta Falcons","shortName":"Atlanta"},

{"code":"WAS","fullName":"Washington Redskins","shortName":"Washington"}

]}

我的代码:import urllib.request as ur

import urllib.parse

import json

url = 'http://www.fantasyfootballnerd.com/service/nfl-teams/json/test/'

user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'

values = {'name' : 'Michael Foord',

'location' : 'Northampton',

'language' : 'Python' }

headers = { 'User-Agent' : user_agent }

data = urllib.parse.urlencode(values)

data = data.encode('ascii')

req = urllib.request.Request(url, data, headers)

response = urllib.request.urlopen(req)

the_page = response.read().decode('utf-8')

print (the_page)

team_data = json.loads(the_page)

print (type(team_data)) // team_data is type dict

for item in team_data:

print (item['NFLTeams'][0]["code"])

print (item['NFLTeams'][0]['fullName'])

print (item['NFLTeams'][0]['shortName'])

我试图打印以下内容:print (item['NFLTeams']["code"])

而这个:for item in team_data['NFLTeams'].values():

print (item["code"])

print (item['fullName'])

print (item['shortName'])

这就给了我一个错误:for item in team_data['NFLTeams'].values():

AttributeError: 'list' object has no attribute 'values'

有谁能帮我弄清楚发生了什么事吗?谢谢您。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值