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

编程入门 行业动态 更新时间:2024-10-17 07:23:06
本文介绍了使用Node.js将文件系统中的目录结构转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个这样的文件结构:

root | _水果 | ___苹果 | ______图像 | ________ apple001.jpg | ________ apple002.jpg | _动物 | ___猫 | ______图像 | ________ cat001.jpg | ________ cat002.jpg

我想使用Javascript和Node .js,听这个根目录和所有的子目录并创建一个镜像这个目录结构的JSON,每个节点包含类型,名称,路径和子节点:

data = [ { type:folder, name:animals, path:/ animals,孩子:[ { type:folder, name:cat, path:/ animals / cat, children:[ { type:folder, name:images, path:/ animals / cat / images, children:[ { type:文件,名称:cat001.jpg,路径:/animals/cat/images/cat001.jpg},{ type:file 名称:cat001.jpg,路径:/animals/cat/images/cat002.jpg} ] } ] } ] } ];

这是一个coffeescript JSON:

data = [ type:folder name:animals path:/ animals children: [类型:文件夹名称:猫路径:/ animals / cat儿童: [类型:$名称:图像路径:/ animals / cat / images儿童: [类型:文件名称:cat001.jpg路径:/animals/cat/images/cat001.jpg,类型:文件名称:cat001.jpg 路径:/animals/cat/images/cat002.jpg] ] ] ]

如何在django视图中获取此json数据格式?(python)

解决方案

这是草图。错误处理作为读者的练习留下。

var fs = require('fs'), path = require('path') 函数dirTree(filename){ var stats = fs.lstatSync(filename), info = { path:filename, name:path.basename(filename)}; if(stats.isDirectory()){ info.type =folder; info.children = fs.readdirSync(filename).map(function(child){ return dirTree(filename +'/'+ child); }); } else { //假设它是一个文件。在现实生活中,它可能是一个符号链接或 //其他的东西! info.type =file; } 返回信息; } if(module.parent == undefined){ // node dirTree.js〜/ foo / bar var util = require('util' ); console.log(util.inspect(dirTree(process.argv [2]),false,null)); }

I have a file structure like this:

root |_ fruits |___ apple |______images |________ apple001.jpg |________ apple002.jpg |_ animals |___ cat |______images |________ cat001.jpg |________ cat002.jpg

I would like to, using Javascript and Node.js, listen to this root directory and all sub directories and create a JSON which mirrors this directory structure, each node contains type, name, path, and children:

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" } ] } ] } ] } ];

Here's a coffeescript 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" ] ] ] ]

how to get this json data format in django views?(python)

解决方案

Here's a sketch. Error handling is left as an exercise for the reader.

var fs = require('fs'), path = require('path') function dirTree(filename) { var stats = fs.lstatSync(filename), info = { path: filename, name: path.basename(filename) }; if (stats.isDirectory()) { info.type = "folder"; info.children = fs.readdirSync(filename).map(function(child) { return dirTree(filename + '/' + child); }); } else { // Assuming it's a file. In real life it could be a symlink or // something else! info.type = "file"; } return info; } if (module.parent == undefined) { // node dirTree.js ~/foo/bar var util = require('util'); console.log(util.inspect(dirTree(process.argv[2]), false, null)); }

更多推荐

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

本文发布于:2023-05-28 06:57:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/315150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   文件系统   结构   目录   Node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!