提问人:Daniel Raviv 提问时间:3/9/2023 更新时间:3/9/2023 访问量:44
将 CSV 转换为 JSON 在 node.js 中无法按计划工作
converting csv into json not working as planned in node.js
问:
嗨,我有这个 CSV 文件,我想让它成为 JSON,这样我就可以在使用 csv-parser 模块node.js轻松访问数据,而不是像文件一样返回 json/对象,它只是返回一个大字符串
'CountryName,CapitalName,CapitalLatitude,CapitalLongitude,CountryCode,ContinentName\n' + '索马里兰,哈尔格萨,9.55,44.050000,NULL,非洲\n' + '南乔治亚岛和南桑威奇群岛,爱德华国王角,-54.283333,-36.500000,GS,南极洲\n' + '法属南部和南极领地,法兰西港,-49.35,70.216667,TF,南极洲\n' + '巴勒斯坦,耶路撒冷,31.76666666666666666,35.233333,PS,亚洲\n' + '奥兰群岛,玛丽港,60.116667,19.900000,AX,欧洲\n' +
这是它返回的示例(第一行是标题)
这是代码
const fs = require('fs');
const csv = require('csv-parser');
const dataResults = [];
const fileHeaders = [
'CountryName',
'CapitalName',
'CapitalLatitude',
'CapitalLongitude',
'CountryCode',
'ContinentName'
]
// reads the file
var readFile = fs.createReadStream(__dirname + '/country-capitals.csv','utf-8');
readFile.pipe(csv({separator: '\n', headers:fileHeaders}))
// event handler that handels the data
readFile.on('data', (data)=>dataResults.push(data))
readFile.on('end',()=>{
console.log(dataResults);
})
const fs = require('fs');
const csv = require('csv-parser');
const dataResults = [];
const fileHeaders = [
'CountryName',
'CapitalName',
'CapitalLatitude',
'CapitalLongitude',
'CountryCode',
'ContinentName'
]
// reads the file
var readFile = fs.createReadStream(__dirname + '/country-capitals.csv','utf-8');
readFile.pipe(csv({separator: '\n', headers:fileHeaders}))
// event handler that handels the data
readFile.on('data', (data)=>dataResults.push(data))
readFile.on('end',()=>{
console.log(dataResults);
})
答:
0赞
Jessica Felix
3/9/2023
#1
如果我了解您想做什么,您可以只使用简单的解析:
法典:
const csv = require('csv-parser')
const fs = require('fs')
const results = [];
const data = (__dirname + '/country-capitals.csv')
fs.createReadStream(data)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});
结果:
[
{
CountryName: 'Somaliland',
CapitalName: 'Hargeisa',
CapitalLatitude: '9.55',
CapitalLongitude: '44.050000',
CountryCode: 'NULL',
ContinentName: 'Africa'
},
{
CountryName: 'South Georgia and South Sandwich Islands',
CapitalName: 'King Edward Point',
CapitalLatitude: '-54.283333',
CapitalLongitude: '-36.500000',
CountryCode: 'GS',
ContinentName: 'Antarctica'
},
{
CountryName: 'French Southern and Antarctic Lands',
CapitalName: 'Port-aux-Français',
CapitalLatitude: '-49.35',
CapitalLongitude: '70.216667',
CountryCode: 'TF',
ContinentName: 'Antarctica'
},
{
CountryName: 'Palestine',
CapitalName: 'Jerusalem',
CapitalLatitude: '31.766666666666666',
CapitalLongitude: '35.233333',
CountryCode: 'PS',
ContinentName: 'Asia'
},
{
CountryName: 'Aland Islands',
CapitalName: 'Mariehamn',
CapitalLatitude: '60.116667',
CapitalLongitude: '19.900000',
CountryCode: 'AX',
ContinentName: 'Europe'
}
]
资料来源: csv-parse doc
但是,如果要更改属性名称,例如将“CountryName”键替换为“Name”,请查看如何重命名 JSON 键
0赞
Evan
3/9/2023
#2
这对我有用。
const csv = require('csv-parser')
const fs = require('fs')
const data = 'CountryName,CapitalName,CapitalLatitude,CapitalLongitude,CountryCode,ContinentName\n' + 'Somaliland,Hargeisa,9.55,44.050000,NULL,Africa\n' + 'South Georgia and South Sandwich Islands,King Edward Point,-54.283333,-36.500000,GS,Antarctica\n'
const results = [];
fs.createReadStream(data)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});
评论