Node.js-将字符串数组转换为对象

时间:2015-09-28 10:04:23

标签: javascript node.js csv

我们将数据库导出到csv,然后使用csv文件将其还原。读取csv文件时,如

0,"SIMry",0,"ROOT",false,true,null

转换为字符串数组。

 ["0","SIMry","0","ROOT","false","true","null"]

Int, NULL, Boolean都失去了意义。我希望输出

[0,"SIMry",0,"ROOT",false,true,null]

我正在使用fast-csv节点库。

1 个答案:

答案 0 :(得分:-1)

使用JSON.parse。例如以下代码

var a= ["0","SIMry",0,"ROOT",false,true,null];
var a_string = JSON.stringify(a); // This is what your library does
var a_json_var = JSON.parse(a_string); // This is how you compensate

console.log (JSON.stringify(a_json_var));
if (a_json_var[5]){console.log ("boolean");}

将打印

["0","SIMry",0,"ROOT",false,true,null]
boolean