1. 程式人生 > >nodejs讀寫json檔案

nodejs讀寫json檔案

nodejs 讀json檔案

var fs=reauire('fs');
var file="d:\\0.json";
var result=JSON.parse(fs.readFileSync( file));
//操作物件

nodejs 寫json檔案

var coors = {};
    coors.left_top = {};
    coors.right_top = {};
    coors.left_bottom = {};
    coors.right_bottom = {};
//填充coors中內容
var filename = "d:\\coors\\0.json"
; fs.writeFileSync(filename, JSON.stringify(coors)); //物件巢狀 var fs = require('fs'); var fileDirectory = "D:\\column"; var newfileDirectory = "d:\\new"; if (fs.existsSync(fileDirectory)) { var files = fs.readdirSync(fileDirectory); for (var i = 0; i < files.length; i++) { var filePath = fileDirectory + "/"
+ files[i]; var newfilepath = newfileDirectory + "/" + files[i] var filestr = JSON.parse(fs.readFileSync(filePath)); for (var item in filestr) { var inter=filestr[item]; for(var m in inter) { inter[m]=parseFloat(inter[m]); } fs.writeFileSync(newfilepath, JSON
.stringify(filestr)); } } } else { console.log(fileDirectory + " Not Found!"); }