[英]How can I fix this JSON TypeError: string indices must be integers?
I have read data from an API and the JSON file looks like this: "{\\"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},...."
我从 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']
I get an error --> TypeError: string indices must be integers.我收到一个错误 --> TypeError: 字符串索引必须是整数。
can someone tell me what I am doing wrong??有人可以告诉我我做错了什么吗??
Apparently its given is an array of dictionaries, try to print the value of data1, so you can see the structure better.显然它给出的是一个字典数组,尝试打印data1的值,这样你就可以更好地看到结构。
import pprint
pprint.pprint(data1)
And try this:试试这个:
results = data1[0]['30days']['dashboard']['total_users']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.