如何使用Node js将csv转换为Json文件?

gg58donl  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(98)

i;m new to node js i need to convert node js file to JSON File it is possible i try

const express = require("express");
const csv = require("csvtojson");
const app = express();
const port = 3000;
const csvFilePath='./csv/HeatMapInput.csv'

app.get("/csvtojson", (req, res) => {
    csv()
    .fromFile(csvFilePath)
    .then((jsonObj)=>{
        const obj = JSON.parse(jsonObj)
     
        res.send(obj)
        
        console.log(jsonObj);
    })
    
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

字符串
输出结果如下:但是我需要

[
[1025,35200855,2019,-79.3821,43.6486,1],
[1025,35570352,2019,-82.7175,46.3916,1],
[1025,35530299,2019,-81.1016,46.6009,1],
[1025,35560281,2020,-81.5358,48.3433,1],
[1025,35530243,2019,-80.9834,46.5063,1],
[1025,35570380,2019,-84.4283,47.6655,1],
[1025,35570342,2019,-82.6412,46.2302,1],
[1025,35230478,2020,-80.8498,43.8323,1],
[1025,35530270,2019,-80.9531,46.6552,1],
[1025,35280226,2019,-79.8006,42.8618,1],
[1025,35201576,2019,-79.5355,43.5952,1],
[1025,60010243,2020,-135.063,60.7208,1],
[1025,35230167,2019,-80.2255,43.5355,1],
[1025,35520100,2020,-82.1531,46.1918,1]


]
可能还是不可能?

brtdzjyr

brtdzjyr1#

在npm中有多个库可用。您可以试用此软件包:https://www.npmjs.com/package/csv-grape

CsvGrape.getCsvData(event).then((_response)=>{
      this.data=_response;
      
    },(reject)=>{
      //log
    });

字符串

ukxgm1gy

ukxgm1gy2#

您所需要的只是安装和使用现有的csv解析器,如csv-parse

const parse = require('csv-parse')
const assert = require('assert')

const input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"'
parse(input, {
  comment: '#'
}, function(err, output){
  assert.deepStrictEqual(
    output,
    [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]
  )
})

字符串

相关问题