【发布时间】:2020-06-12 02:38:52
【问题描述】:
我有一个名为 thefile.json 的 json,它看起来像这样:
{
"domain": "Something",
"domain": "Thingie",
"name": "Another",
"description": "Thing"
}
我正在尝试编写一个 python 脚本,它会在域中生成一组值。在此示例中,它将返回
{'Something', 'Thingie'}
这是我尝试过的:
import json
with open("thefile.json") as my_file:
data = json.load(my_file)
ids = set(item["domain"] for item in data.values())
print(ids)
我收到错误消息
unique_ids.add(item["domain"])
TypeError: string indices must be integers
查看堆栈交换的答案后,我很难过。为什么我不能将字符串作为索引,因为我正在使用数据类型为字典的 json(我认为!)?如何获取它以便我可以获取“域”的值?
【问题讨论】:
-
你的item是一个列表,你需要传递一个整数/索引来访问列表中的字符串