相关疑难解决方法(0)

使用Node.js将文件系统中的目录结构转换为JSON

我有这样的文件结构:

root
|_ fruits
|___ apple
|______images
|________ apple001.jpg
|________ apple002.jpg
|_ animals
|___ cat
|______images
|________ cat001.jpg
|________ cat002.jpg
Run Code Online (Sandbox Code Playgroud)

我想,使用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"
              }
            ]
          }
        ]
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

这是一个coffeescript JSON:

data = 
[
  type: …
Run Code Online (Sandbox Code Playgroud)

javascript filesystems node.js d3.js

57
推荐指数
3
解决办法
6万
查看次数

标签 统计

d3.js ×1

filesystems ×1

javascript ×1

node.js ×1