【发布时间】:2012-06-27 00:01:12
【问题描述】:
我有这样的文件结构:
root
|_ fruits
|___ apple
|______images
|________ apple001.jpg
|________ apple002.jpg
|_ animals
|___ cat
|______images
|________ cat001.jpg
|________ cat002.jpg
我想使用 Javascript 和 Node.js,监听这个根目录和所有子目录,并创建一个反映这个目录结构的 JSON,每个节点都包含类型、名称、路径和子节点:
data = [
{
type: "folder",
name: "animals",
path: "/animals",
children: [
{
type: "folder",
name: "cat",
path: "/animals/cat",
children: [
{
type: "folder",
name: "images",
path: "/animals/cat/images",
children: [
{
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat001.jpg"
}, {
type: "file",
name: "cat001.jpg",
path: "/animals/cat/images/cat002.jpg"
}
]
}
]
}
]
}
];
这是一个咖啡脚本 JSON:
data =
[
type: "folder"
name: "animals"
path: "/animals"
children :
[
type: "folder"
name: "cat"
path: "/animals/cat"
children:
[
type: "folder"
name: "images"
path: "/animals/cat/images"
children:
[
type: "file"
name: "cat001.jpg"
path: "/animals/cat/images/cat001.jpg"
,
type: "file"
name: "cat001.jpg"
path: "/animals/cat/images/cat002.jpg"
]
]
]
]
如何在 django 视图中获取这种 json 数据格式?(python)
【问题讨论】:
-
这是获取 d3.js 分层数据的常见需求。我想用 d3.js 标记这个问题,但 Stack Overflow 最多允许 5 个 :(
-
我希望这些答案之一能够从标准输入读取路径,以便您可以将路径列表转换为 json 对象,如下所示:
find | paths2json。这将通过管道充分利用 Unix 可组合性的强大功能。
标签: javascript node.js d3.js filesystems