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
运行:

nodejs 读取JSON文件写入txt中 fs.readFile&fs.writeFile_第1张图片

再来看result.txt;

nodejs 读取JSON文件写入txt中 fs.readFile&fs.writeFile_第2张图片

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