将文件夹结构加载到NodeJS中的树视图JSON对象

时间:2016-07-26 11:19:03

标签: json node.js file treeview

我想将包含文件夹,json文件及其内容的文件夹结构映射到类似JSON对象的树视图中。我想问一下是否有人会向我提供一个函数,即loadFolder('/ a'),它将加载以下文件夹结构......

/ A

/a/file1.json

/a/file2.json

/a/b/file1.json

/ A / B / C

...会产生这个JSON对象:

    {
        path: "/a",
        name: "a",
        type: "folder",
        subnodes: [
            { 
                path: "/a/file1.json",
                name: "file1.json",
                type: "file",
                content: { 
                    // file content here
                }
            },
            { 
                path: "/a/file2.json",
                name: "file2.json",
                type: "file",
                content: { 
                    // file content here
                }
            },  
            {
                path: "/a/b",
                name: "b",
                type: "folder",
                subnodes: [
                    {
                        path: "/a/b/file1.json",
                        name: "file1.json",
                        type: "file",
                        content: { 
                            // file content here

                        }
                    },
                    {
                        path: "/a/b/c",
                        name: "c",
                        type: "folder",
                        subnodes: []
                    }           
                ]
            }
        ]
    }

1 个答案:

答案 0 :(得分:0)

使用例如this模块,您应该轻松获取文件夹结构。有了它,只需遍历数组查找文件,并使用内置的fs模块打开每个文件。