从节点文件夹中获取所有json文件,并在里面find具体的attr

我已经在我的节点应用程序文件夹与几个JSON文件(可以多于10),我需要从validation方面来阅读它们,并find具体的属性,如果这个属性发生在多个JSON文件抛出一个错误,什么是从性能和效率方面做到最好的方法

例如我的文件夹称为插件和所有的JSON都像下面一样构build

json1

{ "action": [ { "delete": { "path": "deleteFile", "providedAction":"Del" }, { "update": { "path": "updateFile", "providedAction":"UPD" } } ] } 

这是有效的jsonprovidedAction =添加不存在于其他json **

json2

 { "action": [ { "add": { "path": "addFile", "providedAction":"Add" } } ] } 

这是不正确的 JSON既然providedAction = UPD的行动已经存在JSON 3

 { "action": [ { { "update": { "path": "updateFile", "providedAction":"UPD" } } ] } 

我需要validation,只是这个JSON有“德尔”的行动,如果多个JSON有这个Trow错误,如何推荐这样做?

好的,这里是代码。 如果你不明白的话让我知道,我很乐意帮助你!

 var glob = require("glob"); var fs = require("fs"); var _inArray = function(needle, haystack) { for(var k in haystack) { if(haystack[k] === needle) { return true; } } return false; } glob("json/*.json", function(err, files) { // read the folder or folders if you want: example json/**/*.json if(err) { console.log("cannot read the folder, something goes wrong with glob", err); } var matters = []; files.forEach(function(file) { fs.readFile(file, 'utf8', function (err, data) { // Read each file if(err) { console.log("cannot read the file, something goes wrong with the file", err); } var obj = JSON.parse(data); obj.action.forEach(function(crud) { for(var k in crud) { if(_inArray(crud[k].providedAction, matters)) { // do your magic HERE console.log("duplicate founded!"); // you want to return here and cut the flow, there is no point in keep reading files. break; } matters.push(crud[k].providedAction); } }) }); }); }); 

JSON 1:

 {"action": [ { "delete": { "path": "deleteFile", "providedAction": "Del" } }, { "update": { "path": "updateFile", "providedAction": "UPD" } } ] } 

JSON 2:

 { "action": [ { "add": { "path": "addFile", "providedAction": "Add" } } ] } 

JSON 3:

 { "action": [ { "update": { "path": "updateFile", "providedAction": "UPD" } } ] } 

也许这样的事情?,享受! npm install glob

JSON 1

 module.exports = { "action": [{ "delete": { "path": "deleteFile", "action":"Del" } }] } 

 (function() { var glob = require("glob"); glob("path/to/*.js", function(er, files) { if(er) return; var x = 0; files.forEach(function(file) { require(file)['action'].forEach(function(act) { if(act.delete.action && act.delete.action == "Del") x++; }); }); if(x > 1) throw new Exception(""); // or something ja! }); })(); 

早上5点没有睡觉,抱歉,如果我犯了错误,我想告诉你的方式只…不复制粘贴! 的xD。

不是我写的最漂亮的代码,但是这里是:

 // Require the nodejs file system library var fs = require('fs'); var path = '/usr/local/var/jsons'; var delCounter = 0; // Readdir reads a path and gives an array of filenames // to the callback handleFiles. fs.readdir(path, handleFiles); function handleFiles (err, files) { if (err) throw err; var i; var jsonFilePattern=/\.[json]+$/i; var fileName; var filePath; // Tells fs to read an utf-8 file. var fileReadOptions = { 'encoding':'utf-8' }; for (i = 0; i < files.length; ++i) { fileName = files[i]; // Check if the file has a .json extension if (fileName.match(jsonFilePattern)) { filePath = path + '/' + fileName; // Open the file as utf-8 and call handleJsonFile back // when done reading. fs.readFile(filePath, fileReadOptions, handleJsonFile); } } } function handleJsonFile (err, data) { if (err) throw err; var dataObject = JSON.parse(data); var i; var action; // Loop through all possible action. for (i = 0; i < dataObject.action.length; ++i) { action = dataObject.action[i]; if (action.delete && action.delete.providedAction && action.delete.providedAction === 'Del') { // If there is a 'Del', add it to the counter. ++delCounter; } } if (delCounter > 1) { throw new Exception('Jsons  not valid.'); } }