我想知道如何打印出这个嵌套字典中的团队名称。
{
"leagueCaption": "Premier League 2016/17",
"matchday": 17,
"standing": [
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/5/5c/Chelsea_crest.svg",
"goalDifference": 24,
"goals": 35,
"goalsAgainst": 11,
"playedGames": 17,
"points": 43,
"rank": 1,
"team": "Chelsea",
"teamId": 61
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/0/0a/FC_Liverpool.svg",
"goalDifference": 21,
"goals": 41,
"goalsAgainst": 20,
"playedGames": 17,
"points": 37,
"rank": 2,
"team": "Liverpool",
"teamId": 64
},
{
"crestURI": "https://upload.wikimedia.org/wikipedia/en/e/eb/Manchester_City_FC_badge.svg",
"goalDifference": 16,
"goals": 36,
"goalsAgainst": 20,
"playedGames": 17,
"points": 36,
"rank": 3,
"team": "ManCity",
"teamId": 65
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/en/5/53/Arsenal_FC.svg",
"goalDifference": 19,
"goals": 38,
"goalsAgainst": 19,
"playedGames": 17,
"points": 34,
"rank": 4,
"team": "Arsenal",
"teamId": 57
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/b/b4/Tottenham_Hotspur.svg",
"goalDifference": 17,
"goals": 29,
"goalsAgainst": 12,
"playedGames": 17,
"points": 33,
"rank": 5,
"team": "Spurs",
"teamId": 73
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"goalDifference": 7,
"goals": 24,
"goalsAgainst": 17,
"playedGames": 17,
"points": 30,
"rank": 6,
"team": "ManU",
"teamId": 66
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/c/c9/FC_Southampton.svg",
"goalDifference": 1,
"goals": 17,
"goalsAgainst": 16,
"playedGames": 17,
"points": 24,
"rank": 7,
"team": "Southampton",
"teamId": 340
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/8/8b/West_Bromwich_Albion.svg",
"goalDifference": 2,
"goals": 23,
"goalsAgainst": 21,
"playedGames": 17,
"points": 23,
"rank": 8,
"team": "West Bromwich",
"teamId": 74
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/f/f9/Everton_FC.svg",
"goalDifference": 0,
"goals": 21,
"goalsAgainst": 21,
"playedGames": 17,
"points": 23,
"rank": 9,
"team": "Everton",
"teamId": 62
},
{
"crestURI": "https://upload.wikimedia.org/wikipedia/de/4/41/Afc_bournemouth.svg",
"goalDifference": -5,
"goals": 23,
"goalsAgainst": 28,
"playedGames": 17,
"points": 21,
"rank": 10,
"team": "Bournemouth",
"teamId": 1044
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/a/a3/Stoke_City.svg",
"goalDifference": -5,
"goals": 19,
"goalsAgainst": 24,
"playedGames": 17,
"points": 21,
"rank": 11,
"team": "Stoke",
"teamId": 70
},
{
"crestURI": "https://upload.wikimedia.org/wikipedia/en/e/e2/Watford.svg",
"goalDifference": -8,
"goals": 21,
"goalsAgainst": 29,
"playedGames": 17,
"points": 21,
"rank": 12,
"team": "Watford",
"teamId": 346
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/e/e0/West_Ham_United_FC.svg",
"goalDifference": -12,
"goals": 19,
"goalsAgainst": 31,
"playedGames": 17,
"points": 19,
"rank": 13,
"team": "West Ham",
"teamId": 563
},
{
"crestURI": "https://upload.wikimedia.org/wikipedia/en/2/2c/Middlesbrough_FC_crest.svg",
"goalDifference": -3,
"goals": 16,
"goalsAgainst": 19,
"playedGames": 17,
"points": 18,
"rank": 14,
"team": "Middlesbrough",
"teamId": 343
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/en/6/63/Leicester02.png",
"goalDifference": -6,
"goals": 23,
"goalsAgainst": 29,
"playedGames": 17,
"points": 17,
"rank": 15,
"team": "Foxes",
"teamId": 338
},
{
"crestURI": "https://upload.wikimedia.org/wikipedia/en/0/02/Burnley_FC_badge.png",
"goalDifference": -12,
"goals": 16,
"goalsAgainst": 28,
"playedGames": 17,
"points": 17,
"rank": 16,
"team": "Burnley",
"teamId": 328
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/b/bf/Crystal_Palace_F.C._logo_%282013%29.png",
"goalDifference": -4,
"goals": 28,
"goalsAgainst": 32,
"playedGames": 17,
"points": 15,
"rank": 17,
"team": "Crystal",
"teamId": 354
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/6/60/AFC_Sunderland.svg",
"goalDifference": -13,
"goals": 15,
"goalsAgainst": 28,
"playedGames": 17,
"points": 14,
"rank": 18,
"team": "Sunderland",
"teamId": 71
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/a/ab/Swansea_City_Logo.svg",
"goalDifference": -17,
"goals": 20,
"goalsAgainst": 37,
"playedGames": 17,
"points": 12,
"rank": 19,
"team": "Swans",
"teamId": 72
},
{
"crestURI": "http://upload.wikimedia.org/wikipedia/de/a/a9/Hull_City_AFC.svg",
"goalDifference": -22,
"goals": 14,
"goalsAgainst": 36,
"playedGames": 17,
"points": 12,
"rank": 20,
"team": "Hull",
"teamId": 322
}
]
}
我期待打印团队。如果这个dict被称为响应,我想我可以这样做:
team = response['standing']['team']
但我不能收到错误。
我在烧瓶上运行它,这是python代码!
基本上我正在尝试打印出一张EPL排行榜!
from flask import Flask, jsonify, render_template, request
import httplib
import json
app = Flask(__name__)
@app.route('/')
def index1():
return render_template('league.html')
@app.route('/League Standing', methods=['GET','POST'])
def show_league():
text = request.form['league']
connection = httplib.HTTPConnection('api.football-data.org')
headers = {'X-Auth-Token': 'mytoken', 'X-Response-Control': 'minified'}
connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers)
response = json.loads(connection.getresponse().read().decode())
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)
答案 0 :(得分:1)
您无法访问"team"
作为密钥,因为dict["standing"]
包含list
值。为了打印所有团队,请执行以下操作:
for standing in my_json['standing']:
print standing['team']
其中my_json
包含您的dict
对象。如果您想获得所有团队的list
,您可以使用 list comprehension 表达式:
[standing['team'] for standing in my_json['standing']]
答案 1 :(得分:0)
在一行中:
points=[r['team']+": "+str(r['points']) for r in response['standing']]
这回归:['切尔西','利物浦','曼城','阿森纳'马刺' ;,' ManU',' Southampton'' West Bromwich' Everton'' Bournemouth',&# 39; Stoke',' Watford',' West Ham',' Middlesbrough',' Foxes'' Burnley&# 39;,'水晶','桑德兰','天鹅'赫尔']
如果要返回teamname:points作为字符串列表:
points={r['team']:r['points'] for r in response['standing']}
如果您想要teamname:points作为字典,您可以使用:
var doWork = function(reqId) {
var result = $q.defer();
$http.get('/api/dowork/' + reqId).then(function(response) {
result.resolve(response.data);
}).catch(function(response) {
result.reject(response);
});
return result;
};
this.doWork = doWork;
this.mediumRequest = function() {
var req = 'medium';//normally do something hard to derive this value
return doWork(req);
};
答案 2 :(得分:-1)
team = response['standing'][0]['team']
[0]
索引用于访问standing
密钥的第一个字典,因为standing
密钥中存在多个字典。