js:使用npm库csvtojson将 CSV 转换为 JSON 字符串
文档
安装
pnpm i csvtojson
示例
import csvToJson from 'csvtojson'; const csvFilePath = 'data.csv'; const json = await csvToJson().fromFile(csvFilePath); console.log(json);
输入数据 data.csv
color,maxSpeed,age "red",120,2 "blue",100,3 "green",130,2
输出结果
[ { color: 'red', maxSpeed: '120', age: '2' }, { color: 'blue', maxSpeed: '100', age: '3' }, { color: 'green', maxSpeed: '130', age: '2' } ]
参考