TypeError:字符串索引必须是来自jSON和Flask的整数错误

时间:2018-01-15 08:32:26

标签: python json flask

我是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 is:

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,但它给我string indices must be integers exception

1 个答案:

答案 0 :(得分:1)

您不应该执行额外的json.dumps步骤:创建表示JSON文件的字符串,这意味着您的json变量是string对象(因此出错)信息)。如果您使用return json["matchday"]更改return response["matchday"],则应该