gpt4 book ai didi

javascript - 使用 js 将 json 转换为 csv

转载 作者:行者123 更新时间:2023-12-04 10:00:45 26 4
gpt4 key购买 nike

我需要将我的 JSON 转换为 CSV 文件,但我不知道如何正确执行。

我已经有了这个代码

      let rows = typeof objArray !== "object" ? JSON.parse(objArray) : objArray;
let header = "";
Object.keys(rows[0]).map(pr => (header += pr + ";"));

let str = "";
rows.forEach(row => {
let line = "";
let columns =
typeof row !== "object" ? JSON.parse(row) : Object.values(row);

columns.forEach(column => {
if (line !== "") {
line += ";";
}
if (typeof column === "object") {
line += JSON.stringify(column);
} else {
line += column;
}
});
str += line + "\r\n";
});
return header + "\r\n" + str;

但我期待一个与我现在不同的结果

wrong result

我需要这个 expected result

  function ConvertToCSV(objArray) {
let rows = typeof objArray !== "object" ? JSON.parse(objArray) : objArray;
let header = "";
Object.keys(rows[0]).map(pr => (header += pr + ";"));

let str = "";
rows.forEach(row => {
let line = "";
let columns =
typeof row !== "object" ? JSON.parse(row) : Object.values(row);

columns.forEach(column => {
if (line !== "") {
line += ";";
}
if (typeof column === "object") {
line += JSON.stringify(column);
} else {
line += column;
}
});
str += line + "\r\n";
});
return header + "\r\n" + str;
}

function downloadCSV () {
let data = [{"am":{"a3":2,"a1":5,"a2":2},"zul":{"mer":1,"spi":1,"aut":1,"inf2":1,"int":1,"infl":1,"les":1},"kaab": {"ka11":6,"ka12":6,"ka10":6},"kg3":"fdsf","ges":1,"alt":3,"kawe":{"ka9":4,"ka7":5,"ka8":5},"kaak":{"ka2":4,"ka3":5,"ka4":5,"ka1":4,"ka5":3,"ka6":5},"kg4":2,"kg1":5,"eink":"","kg2":4,"soz2":"","solz":"2","ae":{"a6":2,"a5":2,"a4":6}}]

let result = ConvertToCSV(data);

let fileToSave = new Blob([result], {
type: "csv",
name: 'data.csv
});

saveAs(fileToSave, 'data.csv);
}
<script src="https://cdn.jsdelivr.net/npm/file-saver@2.0.2/dist/FileSaver.min.js"></script>
<button onclick="downloadCSV">download csv</button>

最佳答案

你可以试试这个

function downloadCSV () {

let arr = [{"am":{"a3":2,"a1":5,"a2":2},"zul":{"mer":1,"spi":1,"aut":1,"inf2":1,"int":1,"infl":1,"les":1},"kaab": {"ka11":6,"ka12":6,"ka10":6},"kg3":"fdsf","ges":1,"alt":3,"kawe":{"ka9":4,"ka7":5,"ka8":5},"kaak":{"ka2":4,"ka3":5,"ka4":5,"ka1":4,"ka5":3,"ka6":5},"kg4":2,"kg1":5,"eink":"","kg2":4,"soz2":"","solz":"2","ae":{"a6":2,"a5":2,"a4":6}},
{"am":{"a3":2,"a1":5,"a2":2},"zul":{"mer":1,"spi":1,"aut":1,"inf2":1,"int":1,"infl":1,"les":1},"kaab": {"ka11":6,"ka12":6,"ka10":6},"kg3":"fdsf","ges":1,"alt":3,"kawe":{"ka9":4,"ka7":5,"ka8":5},"kaak":{"ka2":4,"ka3":5,"ka4":5,"ka1":4,"ka5":3,"ka6":5},"kg4":2,"kg1":5,"eink":"","kg2":4,"soz2":"","solz":"2","ae":{"a6":2,"a5":2,"a4":6}}
];

var keys = [];
var values = [];
function getKeys(data, k = '') {
for (var i in data) {
var rest = k.length ? '_' + i : i
if (typeof data[i] == 'object') {
if (!Array.isArray(data[i])) {
getKeys(data[i], k + rest)
}
} else keys.push( k+ rest)
}
}
function getValues(data, k = '') {
for (var i in data) {
var rest = k.length ? '' + i : i
if (typeof data[i] == 'object') {
if (!Array.isArray(data[i])) {
getValues(data[i], k + rest)
}
}
else values.push(data[rest])
}
}

getKeys(arr[0])
var value="";
arr.forEach(x=>{
values=[];
getValues(x);
value+=values.join(";")+"\r\n";
})

let result = keys.join(";")+"\r\n"+value;
let fileToSave = new Blob([result], {
type: "csv",
name: 'data.csv'
});

saveAs(fileToSave, 'data.csv');
}
<script src="https://cdn.jsdelivr.net/npm/file-saver@2.0.2/dist/FileSaver.min.js"></script>
<button onclick="downloadCSV()">download csv</button>

关于javascript - 使用 js 将 json 转换为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61835442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com