dek*_*kio 9 python json dictionary
我正在 Python 中使用 json 文件,我想将其转换为字典。
这就是我的文件的样子:
[
{
"label": "Label",
"path": "/label-path",
"image": "icon.svg",
"subcategories": [
{
"title": "Main Title",
"categories": {
"column1": [
{
"label": "Label Title",
"path": "/Desktop/Folder"
}
]
}
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
(抱歉身份问题)
这就是我所做的:
import json
# Opening JSON file
f = open('file.json')
# returns JSON object as
# a dictionary
data = json.load(f)
Run Code Online (Sandbox Code Playgroud)
然而,数据现在是一个列表,而不是一个字典。我试图考虑如何将其转换为字典,但(我)不知道该怎么做;(II) 没有办法将文件作为字典导入吗?
您的数据以列表形式导入,因为在 JSON 文件中,主要结构是数组(方括号),这与 Python 中的列表相当。
如果你只想要内心的指令,你可以这样做
data = json.load(f)[0]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9848 次 |
最近记录: |