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


以上这篇nodejs读写json文件的简单方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。