javascript - 如何正确使用json-2-csv Node 模块?

标签 javascript node.js json csv

我是 nodeJS 的新手所以也许我做错了什么但我正在尝试使用 Node 模块 json-2-csv .但是当我遵循文档并尝试使用它时,我的结果是未定义的。

这是我用测试数据试过的代码。此外,我已经运行了 npm install json-2-csv 并检查了我的 package.json 以查看它是否存在(版本 3.5.4)

let converter = require('json-2-csv');


var array = [
    {
        "configurationId": {
        "id": "4000",
        "displayName": "4000",
        "uri": "/configuration/users/4000"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4001",
        "displayName": "4001",
        "uri": "/configuration/users/4001"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    },
    {
        "configurationId": {
        "id": "4002",
        "displayName": "40002",
        "uri": "/configuration/users/4002"
        },
        "actualValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ],
        "inheritedValue": [],
        "effectiveValue": [
            {
            "id": "Agent",
            "displayName": "Agent",
            "uri": "/configuration/roles/Agent"
            },
            {
            "id": "SMS",
            "displayName": "SMS",
            "uri": "/configuration/roles/SMS"
            }
        ]
    }
    ];

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);
console.log("result is...", result);

重要的一点可能是这里的这一行:

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
}, expandArrayObjects = true);

这是我的输出:

Info: Start process (12:03:39 p.m.)
the result is... undefined
Error:  null
Info: End process (12:03:39 p.m.)

我希望输出是 csv 格式的文本,但正如您从上面看到的那样,我没有定义。我认为我没有正确使用它,但我并没有真正了解这些东西的经验来理解我的错误。

最佳答案

.json2csv 的参数是(csv, function, options)。根据 [npm 来源]

options - (Optional) A JSON document specifying any of the following key value pairs:

因此,代码将是:

var result = converter.json2csv(array, function(err, csv){
    console.log("Error: ", err);
    console.log("Result: ", csv);
}, {expandArrayObjects:true});

如果您想使用其他设置,options 对象将是:

{
    expandArrayObjects: true,
    ....
    ....
}

但我不确定这是否可行,不是因为语法,而是因为我怀疑 json2csv 能否将深度 json 转换为 CSV。

另外,result 不会有数据,因为数据是通过回调传递的,它是一个异步函数。您需要 console.log 它或使用回调中的变量。

关于javascript - 如何正确使用json-2-csv Node 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56584647/

相关文章:

javascript - 在模型上生成自定义 Backbone 错误。保存成功

node.js - 在 nodejs socket.io 中显示连续连接消息

json - 使用 Hamcrest 和 JsonPath : How count the body size? 的 Spring MVC 测试(不计算成员)

javascript - 为什么 JSON.parse 删除双引号?

c# - 使用自定义 XSLT 将 XML 转换为 JSON 会丢失大括号

javascript - 使用 Javascript 设置访问链接的颜色

javascript - 从 Axios GET 请求访问数据

node.js - 如何使用 Sequelize 的 findOrCreate() 捕获错误

sql-server - 命名实例的 mssql Node 问题

javascript - bootstrap 3 datetimepicker 自定义年份