将json转换为csv时在字符串中转义分号

时间:2019-07-10 10:32:04

标签: javascript json csv

我正在尝试将以下数据转换为csv格式,其标头等于json中的键。

data= { 
        title: 'sample title',
        problem: 'this sentence; is a problem',  //<----
        name: 'Anitta',
        albumArt:'sample.png',
      }

我使用csv-writer将json转换为csv。

我的问题是,在生成csv时,带有分号的字符串被拆分并插入2个单独的单元格中。相反,我在problem列下需要它。

预期结果

title        | problem                     | name   | albumArt
-------------|-----------------------------|--------|------------
sample title | this sentence; is a problem | Anitta | sample.png 

当前结果

title        | problem        | name         | albumArt |
-------------|----------------|--------------|----------|----------
sample title | this sentence  | is a problem | Anitta   | sample.png

有什么办法可以解决这个问题?

编辑

for (var key in data) 
  header.push({ id: key, title: key });
  const csvWriter = createCsvWriter({
                path: `./output/${folderName}/${folderName}.csv`,
                header: header
            });
  csvWriter.writeRecords(data) // returns a promise
        .then(() => {
                console.log('Csv file created at :' + 
                `./output/${folderName}/${folderName}.csv`);
                resolve();
            });

1 个答案:

答案 0 :(得分:-3)

很容易尝试一下

data= { 
        title: 'sample title',
        problem: '"this sentence; is a problem"',  //<----
        name: 'Anitta',
        albumArt:'sample.png',
      }