使用JavaScript将Json文件转换为CSV

时间:2019-04-22 02:50:04

标签: javascript json csv

我的项目有问题,我必须使用javascript将json文件转换为csv,但我不知道该怎么做。

你能帮我吗?

我已经尝试过在此站点上找到此代码,但是它对我不起作用,因为它向我显示了此代码,但向我显示了该代码:

place,character,linename,warityu,warityu_kaigyo,elements,gojunelements,kanaelements [object Object],自,巻4:1オ03,false,false,[object Object],[object Object],[object Object] [object Object],陑,巻4:1オ03,false,false,[object Object],,[object Object]

编辑:这是我想要的csv结构:lineNumber,columnNumber,字符,行名,warityu,warityu_kaigyo,x,y,样式,标记,样式,标记,targetLenght,position,positionText,样式,文本,

这是代码:

  "place" : {
    "lineNumber" : 3,
    "columnNumber" : 8
  },
  "character" : "自",
  "linename" : "巻4:1オ03",
  "warityu" : false,
  "warityu_kaigyo" : false,
  "elements" : [ {
    "position" : {
      "x" : 0.0,
      "y" : 2.0
    },
    "style" : "朱",
    "mark" : "・"
  } ],
  "gojunelements" : [ {
    "style" : "墨",
    "mark" : "レ"
  } ],
  "kanaelements" : [ {
    "targetLength" : 1,
    "position" : 0,
    "positionText" : "右",
    "style" : "墨",
    "text" : "ヨリ"
  } ]
}, {
  "place" : {
    "lineNumber" : 3,
    "columnNumber" : 9
  },
  "character" : "陑",
  "linename" : "巻4:1オ03",
  "warityu" : false,
  "warityu_kaigyo" : false,
  "elements" : [ {
    "position" : {
      "x" : -2.0,
      "y" : 2.0
    },
    "style" : "墨",
    "mark" : "∞"
  } ],
  "gojunelements" : [ ],
  "kanaelements" : [ {
    "targetLength" : 1,
    "position" : 0,
    "positionText" : "右",
    "style" : "墨",
    "text" : "シ"
  } ]
}] 
function toCSV(json) {
  var csv = "";
  var keys = (json[0] && Object.keys(json[0])) || [];
  csv += keys.join(',') + '\n';
  for (var line of json) {
    csv += keys.map(key => line[key]).join(',') + '\n';
  }
  return csv;
}

console.log(toCSV(json));

0 个答案:

没有答案