要表示文件结构为JSON,可以使用以下代码示例:
import json
# 构建文件结构
file_structure = {
"root": {
"folder1": {
"file1.txt": "This is file 1",
"file2.txt": "This is file 2"
},
"folder2": {
"file3.txt": "This is file 3",
"folder3": {
"file4.txt": "This is file 4"
}
}
}
}
# 将文件结构转换为JSON字符串
json_data = json.dumps(file_structure, indent=4)
# 打印JSON字符串
print(json_data)
运行以上代码,将得到文件结构的JSON表示:
{
"root": {
"folder1": {
"file1.txt": "This is file 1",
"file2.txt": "This is file 2"
},
"folder2": {
"file3.txt": "This is file 3",
"folder3": {
"file4.txt": "This is file 4"
}
}
}
}
可以看到,文件结构被转换为具有嵌套关系的JSON对象。
上一篇:标示未执行任务的日期。
下一篇:表示x的最少位数的两个补码形式。