0

我;我是节点 js 的新手,我需要将节点 js 文件转换为 JSON 文件,我可以尝试

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}`);
});

输出结果如下:[{"DAUID":"24770084","nbvisitors":"5","longitude":"-74.148441882693675","latitude":"45.8931201053586"},{"DAUID":"24650304","nbvisitors":"5","longitude":"-73.734375249335187","latitude":"45.540948335709679"},{"DAUID":"24661491","nbvisitors":"5","longitude":"-73.606149772501126","latitude":"45.55292693893162"},{"DAUID":"24650268","nbvisitors":"5","longitude":"-73.724700903473192","latitude":"45.614324278005938"},{"DAUID":"24640198","nbvisitors":"5","longitude":"-73.69798080966396","latitude":"45.717128360196966"},{"DAUID":"24663434","nbvisitors":"5","longitude":"-73.574534965366126","latitude":"45.4894264593454"},{"DAUID":"24663120","nbvisitors":"5","longitude":"-73.948554611068985","latitude":"45.406642321361936"},{"DAUID":"24490137","nbvisitors":"5","longitude":"-72.539540694503245","latitude":"45.9086501375339"},{"DAUID":"24661397","nbvisitors":"5","longitude":"-73.609805800126836","latitude":"45.515693042999608"},{"DAUID":"24290072","nbvisitors":"5","longitude":"-70.6713468499011","latitude":"46.1338224450566"}] 但我需要

[
[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]

]

有没有可能?

4

1 回答 1

-1

您只需要安装和使用现有的 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' ] ]
  )
})
于 2021-06-12T07:45:21.053 回答