我已从API读取数据,并且JSON文件如下所示:
"{\"30days\":{\"dashboard\":{\"total_sessions\":{\"total\":12420,\"change\":\"2.4%\",\"trend\":\"u\"},\"new_users\":{\"total\":1255,\"change\":\"-6.6%\",\"trend\":\"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:字符串索引必须是整数。
有人可以告诉我我在做什么错吗?
答案 0 :(得分:1)
显然给定的是一个字典数组,请尝试打印data1的值,以便更好地查看结构。
import pprint
pprint.pprint(data1)
然后尝试:
results = data1[0]['30days']['dashboard']['total_users']