新手求教 node.js 操作JSON
发布网友
发布时间:2022-05-15 02:26
我来回答
共1个回答
热心网友
时间:2022-05-15 03:56
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!");
}