我的JSON对象是换行符分隔的,看起来像这样:
Document
编辑:我需要节点从JSON文件中读取换行符。不输出换行符。它是一个非常大的JSON文件,其中每个对象由换行符分隔,而不是'
答案 0 :(得分:0)
看起来你不需要分隔符字段。尝试
json2csv({ data: myCars, fields: fields }, function(err, csv) { ... }
文档示例:https://github.com/zemirco/json2csv
var json2csv = require('json2csv');
var fields = ['car', 'price', 'color'];
var myCars = [
{
"car": "Audi",
"price": 40000,
"color": "blue"
}, {
"car": "BMW",
"price": 35000,
"color": "black"
}, {
"car": "Porsche",
"price": 60000,
"color": "green"
}
];
json2csv({ data: myCars, fields: fields }, function(err, csv) {
if (err) console.log(err);
fs.writeFile('file.csv', csv, function(err) {
if (err) throw err;
console.log('file saved');
});
});
创建输出文件:
car, price, color
"Audi", 40000, "blue"
"BMW", 35000, "black"
"Porsche", 60000, "green"