nodejs 读取JSON文件写入txt中 fs.readFile&fs.writeFile

读取JSON格式文件中的数据,然后写入到一个文本文件中

book.json:

[
{"name":"book1","category":"1","quantity":"27","allowlend":"1"},
{"name":"book2","category":"2","quantity":"27","allowlend":"1"},
{"name":"book3","category":"3","quantity":"27","allowlend":"1"},
{"name":"book4","category":"4","quantity":"27","allowlend":"1"},
{"name":"book5","category":"5","quantity":"27","allowlend":"1"}
]

json.js

var fs=require('fs');

fs.readFile('json/book.json',function(err,data){
	if(err)
		throw err;
		
	var jsonObj=JSON.parse(data);
	var space=' ';
	var newLine=' . ';
	var chunks=[];
	var length=0;
	
	for(var i=0,size=jsonObj.length;i
运行:

再来看result.txt;



你可能感兴趣的:(Node.js,杂货)