gpt4 book ai didi

python - 类型错误 : string indices must be integers error from jSON and Flask

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:51 24 4
gpt4 key购买 nike

我是Python编程的新手。这花了我很长时间,但我仍然无法调试它。我想做的是访问一个 JSON 对象,但不幸的是它返回了一个错误给我。

这是来自 RESTFUL API 的示例 JSON:

{"matchday": 23, "standing": [{"teamId": 65, "goalsAgainst": 17, "points": 62, "playedGames": 23, "crestURI": "https://upload.wikimedia.org/wikipedia/en/e/eb/Manchester_City_FC_badge.svg", "rank": 1, "goals": 67, "goalDifference": 50, "team": "ManCity"}, {"teamId": 66, "goalsAgainst": 16, "points": 47, "playedGames": 22, "crestURI": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg", "rank": 2, "goals": 45, "goalDifference": 29, "team": "ManU"}, ...

我的app.py是:

def search_team():
import http.client
import json

#http://api.football-data.org/v1/teams/66
connection = http.client.HTTPConnection('api.football-data.org')
headers = { 'X-Auth-Token': 'c4c0ba9c685041aca2fase3d1b2fa5e585', 'X-Response-Control': 'minified' }
connection.request('GET', '/v1/competitions/445/leagueTable', None, headers )
response = json.loads(connection.getresponse().read().decode('utf-8'))
json = json.dumps(response)

return json["matchday"]

if __name__ == '__main__':
app.run()

我期望输出:23,但它给我字符串索引必须是整数异常

最佳答案

您不应该执行额外的 json.dumps 步骤:创建一个表示 JSON 文件的字符串,这意味着您的 json 变量是一个 string 对象(因此出现错误消息)。如果您将 return json["matchday"] 更改为 return response["matchday"],它应该可以工作

关于python - 类型错误 : string indices must be integers error from jSON and Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com