当前位置:主页 > 脚本语言 > NodeJS >

nodejs读写json文件的简单方法(必看)

时间:2020-12-14 14:32:43 | 栏目:NodeJS | 点击:

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!");
}


您可能感兴趣的文章:

相关文章