gpt4 book ai didi

python - 如何修复此 JSON TypeError : string indices must be integers?

转载 作者:行者123 更新时间:2023-12-01 08:23:39 26 4
gpt4 key购买 nike

我已从 API 读取数据,JSON 文件如下所示:"{\"30days\":{\"dashboard\":{\"total_sessions\":{\"total\":12420,\"change\":\"2.4%\",\"趋势\":\"u\"},\"new_users\":{\"总计\":1255,\"变化\":\"-6.6%\",\"趋势\":\"d\"},\"total_users\":{\"total\":1466,\"change\":\"-6.3%\",\"trend\":\"d\",\"is_estimate\":false },...."

 with open ("data_file.json", "r") as f:
data1 = json.load(f)

results = data1['30days']['dashboard']['total_users']

我收到错误 --> TypeError: 字符串索引必须是整数。

有人可以告诉我我做错了什么吗?

最佳答案

显然它给出的是一个字典数组,尝试打印data1的值,这样你可以更好地看到结构。

import pprint
pprint.pprint(data1)

试试这个:

results = data1[0]['30days']['dashboard']['total_users']

关于python - 如何修复此 JSON TypeError : string indices must be integers?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54444848/

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