我如何 select Flask 中 api 响应的特定部分

How do i select a specific part of a api response in flask

代码:

@app.route("/")
def home():
    
    connection = http.client.HTTPConnection('api.football-data.org')
    headers = { 'X-Auth-Token': 'My api key' }
    connection.request('GET', 'https://api.football-data.org/v2/competitions/CL/matches?status=FINISHED', None, headers )
    response = json.loads(connection.getresponse().read().decode())

    return response

回复:

{
"competition": {
"area": {
"id": 2077,
"name": "Europe"
},
"code": "CL",
"id": 2001,
"lastUpdated": "2022-03-20T09:20:44Z",
"name": "UEFA Champions League",
"plan": "TIER_ONE"
},
"count": 216,
"filters": {
"status": [
"FINISHED"
]
},
"matches": [
{
"awayTeam": {
"id": 8912,
"name": "Inter Club d'Escaldes"
},
"group": null,
"homeTeam": {
"id": 8166,
"name": "HB Tórshavn"
},
"id": 328846,
"lastUpdated": "2022-05-04T16:20:11Z",
"matchday": null,
"referees": [],
"score": {
"duration": "REGULAR",
"fullTime": {
"awayTeam": 1,
"homeTeam": 0
},

"winner": "AWAY_TEAM"
}

我删除了一些回复,这样我就可以post回答这个问题

这不是完整的回复,它只是其中的一部分 我想获取 awayTeam 和 homeTeam 名称,但我不知道,当我尝试时

return response.matches[0].awayTeam.name

它给我一个错误说

AttributeError: 'dict' 对象没有属性 'matches'

如果 response 是一个字典对象,尝试查看它的键:

print(response.keys()) 

然后使用这些键访问字典中的值

matches = response['matches']
example_match = response['matches'][0]