简体   繁体   English

如何从npm包“ csvtojson”中迭代对象

[英]How to iterate an object from the npm package “csvtojson”

I have a problem converting my csv to json with the npm package "csvtojson". 我在使用npm包“ csvtojson”将csv转换为json时遇到问题。 First, I converted it into an ascii character set, then I used the conversion to utf16 offered by the package, but now they appear to me as "[object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object], [object Object] " 首先,我将其转换为ascii字符集,然后使用了程序包提供的对utf16的转换,但是现在它们在我看来是“ [object Object],[object Object],[object Object],[object Object] ,[对象对象],[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]“

I tried to iterate the object, but without success. 我试图迭代该对象,但没有成功。 I need your help. 我需要你的帮助。 Then I leave an image of the csv. 然后,我留下了csv的图像。

在此处输入图片说明

const csvFilePath='./download/negocio.csv'
const csv=require('csvtojson');

app.get('/api', async(req, res) => {
  var jsonArray=await csv().fromFile(csvFilePath);
  jsonArray =  jsonArray.toString('utf16');

  res.json(jsonArray);

});

Try this first 先尝试一下

const utils = require('../helper/util');

const path = './download/negocio.csv';

app.get('/api', async (req, res) => {
  const result = await utils.csvToJson(path);
  return res.status(200).json(res);
});

utils.js utils.js

const csv = require('csvtojson');

const utils = {};

utils.csvToJson = async (path) => {
  try {
    const jsonArray = await csv().fromFile(path);
    return jsonArray;
  } catch (err) {
    throw err;
  }
};

module.exports = utils;

Note keeps eyes on the path. 注意使您始终注视着路径。 The path ./downloads/.. can be different for one file to another file. 一个文件与另一个文件的路径./downloads/..可以不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM