JSON输出格式

时间:2015-08-26 10:56:08

标签: json

我正在尝试通过节点js格式化JSON输出,如下所示

[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interface","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}

但目前我正在获得如下所示的输出

[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interface","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}

请查找我使用的代码段:可能知道代码中所需的更改::

var json_string=JSON.stringify(rows,null,2);
            var json_object=setValue(JSON.parse(json_string));
            if (!err){
                console.log('The  object are returning ',json_object);
                var result = _.map(json_object, function(o) {
                    return {headers: _.keys(o), values : _.values(o)}
                });

1 个答案:

答案 0 :(得分:0)

您的问题似乎来自我回答的其他帖子:

How to format the JSON object key/value pair

var json_string=JSON.stringify(rows,null,2);
var json_object=setValue(JSON.parse(json_string));
var result;

if (!err){
  console.log('The  object are returning ',json_object);
  if (_.isArray(json_object) && json_object.length > 0) {
    result = {
      headers: _.keys(json_object[0]),
      values: _.map(json_object, function(o) {
        return _.values(o);
      })
    };
  }
}