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!"); }
上一篇:node.JS的crypto加密模块使用方法详解(MD5,AES,Hmac,Diffie-Hellman加密)
栏 目:NodeJS
下一篇:详解使用Typescript开发node.js项目(简单的环境配置)
本文地址:http://www.codeinn.net/misctech/32596.html