我想将JSON响应转换为CSV格式。我能够弄清楚如何正确地生成标头,其中将键的父级附加在一起以生成合适的标头。我遇到的问题是构造与每一行相对应的数据。例如,假设我有名为id
,tags
和friends_id
的这些列。以id = 1, tags = [car, plane] friends_id = [0,1,2]
为例。 CSV格式的表格应该看起来像这样。
+-------+-------+------------+
| id | tags | friends_id |
+-------+-------+------------+
| 1 | car | 0 |
+-------+-------+------------+
| empty | plane | 1 |
+-------+-------+------------+
| empty | empty | 2 |
+-------+-------+------------+
| 2 | ... | ... |
+-------+-------+------------+
这是我正在处理的一些测试数据。
{
"_id": "5cfe7d3c6deeeef08ce0444b",
"name": "Debra Milligain",
"phone": "+1 (906) 432-2182",
"address": "676 Merit Court, Steinhatchee, Oregon, 5491",
"tags": [
"consequat",
"reprehenderit",
"amet"
],
"Work": {
"skills": [{
"id": 0,
"name": "Programming"
},
{
"id": 1,
"name": "Business"
}
]
},
"friends": [{
"id": 0,
"name": "Stafford Hernandez"
},
{
"id": 1,
"name": "Colleen Christensen"
},
{
"id": 2,
"name": "Barker Keith"
}
],
"greeting": [],
"favoriteFruit": "banana"
}
以下代码(这是我遇到的一个较早问题的解决方案,发现here是我用来递归地将标头生成到映射表或哈希表中并附加其对应值的方法。
var arrayOfHeaders = {};
var headerDirectory = "";
var rootLevel = true;
var temp = ""
var firstWalkthrough = true;
function traverseJSON(obj){
for (var o in obj) {
if (typeof obj[o] == "object") {
//console.log("Before traversal ", o)
//console.log("Traversing the object: ", obj[o])
if(!isNaN(o)){
//console.log("Current position is a number ", o)
}else{
console.log("Adding to directory... " , o)
headerDirectory += (headerDirectory == "") ? o : "_" + o;
}
rootLevel = false;
if (firstWalkthrough){
firstWalkthrough = false;
//if (o == 0)
rootLevel = true;
}
traverseJSON(obj[o]);
rootLevel = true;
temp = headerDirectory;
headerDirectory = "";
} else {
if (rootLevel) {
if(isNaN(o)){
headerDirectory = "";
//console.log("Has value and is root ", o, "and temp ", temp)
arrayOfHeaders[o] += ",\"" + obj[o] + "\"";
}else{
arrayOfHeaders[headerDirectory+"_"+o] += ",\"" + obj[o] + "\"";
}
}
else {
//console.log("Has value and is not root ", o)
//console.log("Current Header Directory " + headerDirectory)
//console.log("Saved temp : ", temp)
if(isNaN(o)){
if(headerDirectory == "") headerDirectory = temp;
//arrayOfHeaders.push(headerDirectory + "_" + o)
arrayOfHeaders[headerDirectory + "_" + o] += ",\"" + obj[o] + "\"";
}
}
}
}
// console.log("Array of Headers : ", arrayOfHeaders)
}
这是来自arrayofHeaders的实际响应:
{ _id: 'undefined,"5cfe7d3c6deeeef08ce0444b"',
name: 'undefined,"Debra Milligain"',
phone: 'undefined,"+1 (906) 432-2182"',
address: 'undefined,"676 Merit Court, Steinhatchee, Oregon, 5491"',
tags_0: 'undefined,"consequat"',
tags_1: 'undefined,"reprehenderit"',
tags_2: 'undefined,"amet"',
'Work_skills_id-skill': 'undefined,"0","Business"',
'Work_skills_name-skill': 'undefined,"Programming"',
'friends_id-friends': 'undefined,"0","1","2"',
'friends_name-friends':
'undefined,"Stafford Hernandez","Colleen Christensen","Barker Keith"',
favoriteFruit: 'undefined,"banana"' }
我想要以一种易于遍历并生成这样的csv的方式来构造数据。问题是我不确定如何实现从上到下的飞跃。
+--------------------------+-----------------+-------------------+---------------------------------------------+---------------+------------------------+--------------------------+---------------------+-----------------------+---------------+
| _id | name | phone | address | tags | Work__skills__id-skill | Work__skills__name-skill | friends__id-friends | friends__name-friends | favoriteFruit |
+--------------------------+-----------------+-------------------+---------------------------------------------+---------------+------------------------+--------------------------+---------------------+-----------------------+---------------+
| 5cfe7d3c6deeeef08ce0444b | Debra Milligain | +1 (906) 432-2182 | 676 Merit Court, Steinhatchee, Oregon, 5491 | consequat | 0 | Programming | 0 | Stafford Hernandez | banana |
+--------------------------+-----------------+-------------------+---------------------------------------------+---------------+------------------------+--------------------------+---------------------+-----------------------+---------------+
| | | | | reprehenderit | Business | | 1 | Colleen Christensen | |
+--------------------------+-----------------+-------------------+---------------------------------------------+---------------+------------------------+--------------------------+---------------------+-----------------------+---------------+
| | | | | amet | | | 2 | Barker Keith | |
+--------------------------+-----------------+-------------------+---------------------------------------------+---------------+------------------------+--------------------------+---------------------+-----------------------+---------------+
编辑: El Tom-您的代码可能已修复
var arrayOfHeaders = {};
var headerDirectory = "";
var rootLevel = true;
var temp = ""
var firstWalkthrough = true;
traverseJSON(
JSON.parse('{"_id": "5cfe7d3c6deeeef08ce0444b","name": "Debra Milligain","phone": "+1 (906) 432-2182","address": "676 Merit Court, Steinhatchee, Oregon, 5491","tags": ["consequat","reprehenderit","amet"],"Work": {"skills": [{"id": 0,"name": "Programming"},{"id": 1,"name": "Business"}]},"friends": [{"id": 0,"name": "Stafford Hernandez"},{"id": 1,"name": "Colleen Christensen"},{"id": 2,"name": "Barker Keith"}],"greeting": [],"favoriteFruit": "banana"}')
)
function traverseJSON(obj) {
for (var o in obj) {
if (typeof obj[o] == "object") {
//console.log("Before traversal ", o)
//console.log("Traversing the object: ", obj[o])
if (!isNaN(o)) {
//console.log("Current position is a number ", o)
} else {
console.log("Adding to directory... ", o)
headerDirectory += (headerDirectory == "") ? o : "_" + o;
}
rootLevel = false;
if (firstWalkthrough) {
firstWalkthrough = false;
//if (o == 0)
rootLevel = true;
}
traverseJSON(obj[o]);
rootLevel = true;
temp = headerDirectory;
headerDirectory = "";
} else {
if (rootLevel) {
if (isNaN(o)) {
headerDirectory = "";
//console.log("Has value and is root ", o, "and temp ", temp)
if (arrayOfHeaders[o] !== undefined) {
arrayOfHeaders[o].push(obj[o]);
} else {
arrayOfHeaders[o] = [obj[o]];
}
} else {
if (arrayOfHeaders[headerDirectory + "_" + o] !== undefined) {
arrayOfHeaders[headerDirectory + "_" + o].push(obj[o]);
} else {
arrayOfHeaders[headerDirectory + "_" + o] = [obj[o]];
}
}
}
else {
//console.log("Has value and is not root ", o)
//console.log("Current Header Directory " + headerDirectory)
//console.log("Saved temp : ", temp)
if (isNaN(o)) {
if (headerDirectory == "") headerDirectory = temp;
//arrayOfHeaders.push(headerDirectory + "_" + o)
if (arrayOfHeaders[headerDirectory + "_" + o] !== undefined) {
arrayOfHeaders[headerDirectory + "_" + o].push(obj[o]);
} else {
arrayOfHeaders[headerDirectory + "_" + o] = [obj[o]];
}
}
}
}
}
}
var res = JSON.stringify(arrayOfHeaders).replace(/,"/g, ',\n"')
console.log("Easier Arrays in Array printed by JSON.stringify:\n", res)
res = {
"_id": ["5cfe7d3c6deeeef08ce0444b"],
"name": ["Debra Milligain"],
"phone": ["+1 (906) 432-2182"],
"address": ["676 Merit Court, Steinhatchee, Oregon, 5491"],
"tags_0": ["consequat"],
"tags_1": ["reprehenderit"],
"tags_2": ["amet"],
"Work_skills_id": [0, 1],
"Work_skills_name": ["Programming",
"Business"],
"friends_id": [0, 1, 2],
"friends_name": ["Stafford Hernandez",
"Colleen Christensen",
"Barker Keith"],
"favoriteFruit": ["banana"]
};
var yourResult = "";
for(var i in arrayOfHeaders) {
if(arrayOfHeaders[i].length > 1) {
arrayOfHeaders[i] = '"' + arrayOfHeaders[i].join('","') + '"';
} else {
arrayOfHeaders[i] = '"' + arrayOfHeaders[i] + '"';
}
yourResult += i + ':' + arrayOfHeaders[i] + '\n';
}
console.log("\nProbably result you wanted to collect but invalid JSON format:\n", yourResult);
但是,如果将它们与我的答案进行比较,则打印的结构是相同的,但与您手动生成的表不同(可能有一些错误)。
答案 0 :(得分:1)
是吗(必须运行该片段以将结果作为HTML表格演示程序显示-不知道如何将其直接放在此处)?
function readDown(headName, arr, outRows) {
var under = [headName];
while (arr.toString().indexOf("[object") == 0
|| Array.isArray(arr)) {
var hasArray = false;
for (var i in arr) {
if (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i])) hasArray = true;
}
if (hasArray) {
for (var i in arr)
if (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i])) {
if (isNaN(i)) under[0] += '_' + i;
readDown(under[0], arr[i], outRows);
if (arr.length === undefined)
return; // empty associative (skills)
delete arr[i++];
var next = false; // are there following arrays to be groupped ?
while (i && arr[i - 1] === undefined && i < arr.length && (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i]))) { // group whole lowest array
next = true;
var idx = outRows.length - 1
if (!Array.isArray(outRows[idx][1])) {
outRows[idx][1] = [outRows[idx][1], arr[i++]];
} else outRows[idx][1].push(arr[i++]);
delete arr[i - 1];
}
if (next && i == arr.length) {
arr.length = 0; // array full of undefined members, fix length in case
return null; // and return - we are done here
}
} else { // funny never get there now
if (hasArray && isNaN(i)) under[0] += '_' + i;
under.push(arr[i]);
break;
}
} else {
if (arr.length === undefined) { // Work_skills[0], friends[0]
under.push(arr);
outRows.push(under);
} else { // tags, greetings
if(!arr.length) return; // do not keep empty greetings
for (var i in arr) under.push(arr[i]);
outRows.push(under);
}
return null; // all arrays return here
}
// unreachable next
}
}
function JSON2CSV(arr, inRows, outRows) {
var row = [];
var hasArray = false;
for (var a in arr) {
try {
if (arr[a] == null) { // Change null's to string
arr[a] = "null";
}
if (Array.isArray(arr[a]) || arr[a].toString().indexOf("[object") == 0) {
readDown(a, arr[a], outRows);
continue;
} else {
if (isNaN(a)) {
outRows.push([a, arr[a]]);
} else {
outRows.push(['#', arr[a]]);
}
}
} catch (e) {
return e;
}
}
}
function convert(json) {
var JSONtxt;
try {
JSONtxt = JSON.parse(json);
} catch (e) {
console.log(e);
return;
}
var columnsArray = [];
JSON2CSV(JSONtxt, [], columnsArray);
var maxDepth = 1; // find longest lowest array size
for (var i in columnsArray)
if (Array.isArray(columnsArray[i][1])
&& maxDepth < columnsArray[i][1].length)
maxDepth = columnsArray[i][1].length;
maxDepth++;
var csv = [];
for (var c in columnsArray) {
if (Array.isArray(columnsArray[c][1])) {
var r = 0;
for (var nd in columnsArray[c][1][r]) {
csv.push([columnsArray[c][0] + '_' + nd]); // top header
var col = csv.length - 1;
for (; r < columnsArray[c][1].length; r++) {
csv[col].push(columnsArray[c][1][r][nd]); // key's value
delete columnsArray[c][1][r][nd];
}
r = 0;
}
} else {
csv.push(columnsArray[c]);
}
while (csv[c].length < maxDepth) csv[c].push(null);
}
var csvTxt = ""; // tab separated values (copy/paste 2 XL ?)
var htmlTab = "<TABLE border=\"1\" width=\"1500\">";
for (var r = 0; r < maxDepth; r++) {
htmlTab += "<TR>";
for (var c in csv) {
htmlTab += r==0?"<TH":"<TD"; // mind 1st header row too
if (c != 0) csvTxt += '\t';
if (csv[c][r] != null) {
csvTxt += csv[c][r];
htmlTab += ">" + csv[c][r];
} else htmlTab += " class=empty>"
htmlTab += "</TD>";
}
csvTxt += '\r\n';
htmlTab += r==0?"</TH>":"</TR>";
}
htmlTab += "</TABLE>";
return htmlTab;
}
var json = '{"_id": "5cfe7d3c6deeeef08ce0444b","name": "Debra Milligain","phone": "+1 (906) 432-2182","address": "676 Merit Court, Steinhatchee, Oregon, 5491","tags": ["consequat","reprehenderit","amet"],"Work": {"skills": [{"id": 0,"name": "Programming"},{"id": 1,"name": "Business"}]},"friends": [{"id": 0,"name": "Stafford Hernandez"},{"id": 1,"name": "Colleen Christensen"},{"id": 2,"name": "Barker Keith"}],"greeting": [],"favoriteFruit": "banana"}';
table {
border-spacing: 0px; /* small tricks 2 make rounded table simply or */
}
th {
text-align:left; /* centered looks ugly */
}
td.empty {
background-color:lightgray; /* mark null cells */
}
<body onload="document.body.innerHTML=convert(json)"></body>
未对代码段进行整洁修改的原始代码
function readDown(headName, arr, outRows) {
var under = [headName];
while (arr.toString().indexOf("[object") == 0
|| Array.isArray(arr)) {
var hasArray = false;
for (var i in arr) {
if (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i])) hasArray = true;
}
if (hasArray) {
for (var i in arr)
if (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i])) {
if (isNaN(i)) under[0] += '_' + i;
readDown(under[0], arr[i], outRows);
if (arr.length === undefined)
return; // empty associative (skills)
delete arr[i++];
var next = false; // are there following arrays to be groupped ?
while (i && arr[i - 1] === undefined && i < arr.length && (arr[i].toString().indexOf("[object") == 0
|| Array.isArray(arr[i]))) { // group whole lowest array
next = true;
var idx = outRows.length - 1
if (!Array.isArray(outRows[idx][1])) {
outRows[idx][1] = [outRows[idx][1], arr[i++]];
} else outRows[idx][1].push(arr[i++]);
delete arr[i - 1];
}
if (next && i == arr.length) {
arr.length = 0; // array full of undefined members, fix length in case
return null; // and return - we are done here
}
} else { // funny never get there now
if (hasArray && isNaN(i)) under[0] += '_' + i;
under.push(arr[i]);
break;
}
} else {
if (arr.length === undefined) { // Work_skills[0], friends[0]
under.push(arr);
outRows.push(under);
} else { // tags, greetings
if(!arr.length) return; // do not keep empty greetings
for (var i in arr) under.push(arr[i]);
outRows.push(under);
}
return null; // all arrays return here
}
// unreachable next
}
}
function JSON2CSV(arr, inRows, outRows) {
var row = [];
var hasArray = false;
for (var a in arr) {
try {
if (arr[a] == null) { // Change null's to string
arr[a] = "null";
}
if (Array.isArray(arr[a]) || arr[a].toString().indexOf("[object") == 0) {
readDown(a, arr[a], outRows);
continue;
} else {
if (isNaN(a)) {
outRows.push([a, arr[a]]);
} else {
outRows.push(['#', arr[a]]);
}
}
} catch (e) {
return e;
}
}
}
function convert(json) {
var JSONtxt;
try {
JSONtxt = JSON.parse(json);
} catch (e) {
console.log(e);
return;
}
var columnsArray = [];
JSON2CSV(JSONtxt, [], columnsArray);
var maxDepth = 1; // find longest lowest array size
for (var i in columnsArray)
if (Array.isArray(columnsArray[i][1])
&& maxDepth < columnsArray[i][1].length)
maxDepth = columnsArray[i][1].length;
maxDepth++;
var csv = [];
for (var c in columnsArray) {
if (Array.isArray(columnsArray[c][1])) {
var r = 0;
for (var nd in columnsArray[c][1][r]) {
csv.push([columnsArray[c][0] + '_' + nd]); // top header
var col = csv.length - 1;
for (; r < columnsArray[c][1].length; r++) {
csv[col].push(columnsArray[c][1][r][nd]); // key's value
delete columnsArray[c][1][r][nd];
}
r = 0;
}
} else {
csv.push(columnsArray[c]);
}
while (csv[c].length < maxDepth) csv[c].push(null);
}
var csvTxt = ""; // tab separated values (copy/paste 2 XL ?)
var htmlTab = "<TABLE border=\"1\">";
for (var r = 0; r < maxDepth; r++) {
htmlTab += "<TR>";
for (var c in csv) {
htmlTab += "<TD>";
if (c != 0) csvTxt += '\t';
if (csv[c][r] != null) {
csvTxt += csv[c][r];
htmlTab += csv[c][r];
}
htmlTab += "</TD>";
}
csvTxt += '\r\n';
htmlTab += "</TR>";
}
htmlTab += "</TABLE>";
console.log(csvTxt);
console.log(htmlTab);
}
var json = '{"_id": "5cfe7d3c6deeeef08ce0444b","name": "Debra Milligain","phone": "+1 (906) 432-2182","address": "676 Merit Court, Steinhatchee, Oregon, 5491","tags": ["consequat","reprehenderit","amet"],"Work": {"skills": [{"id": 0,"name": "Programming"},{"id": 1,"name": "Business"}]},"friends": [{"id": 0,"name": "Stafford Hernandez"},{"id": 1,"name": "Colleen Christensen"},{"id": 2,"name": "Barker Keith"}],"greeting": [],"favoriteFruit": "banana"}';
convert(json);
甚至我原来的JSONoperations.hta CSV导出看上去也不错,但是有点旋转了,包括了无用的1级编号,无论如何,如果我从这里开始;-)
table { border-spacing: 0px; }
th { text-align:left; }
td.empty { background-color:lightgray;}
<TABLE border="1" width="900"><TR><TD>tags</TD><TD>0</TD><TD>1</TD><TD>2</TD></TR><TR><TD class="empty"></TD><TD>consequat</TD><TD>reprehenderit</TD><TD>amet</TD></TR><TR><TD>Work</TD><TD>skills</TD><TD>#</TD><TD>id</TD><TD>name</TD></TR><TR><TD class="empty"></TD><TD class="empty"></TD><TD>1</TD><TD>0</TD><TD>Programming</TD></TR><TR><TD class="empty"></TD><TD class="empty"></TD><TD>2</TD><TD>1</TD><TD>Business</TD></TR><TR><TD>friends</TD><TD>#</TD><TD>id</TD><TD>name</TD></TR><TR><TD class="empty"></TD><TD>1</TD><TD>0</TD><TD>Stafford Hernandez</TD></TR><TR><TD class="empty"></TD><TD>2</TD><TD>1</TD><TD>Colleen Christensen</TD></TR><TR><TD class="empty"></TD><TD>3</TD><TD>2</TD><TD>Barker Keith</TD></TR><TR><TD>_id</TD><TD>name</TD><TD>phone</TD><TD>address</TD><TD>favoriteFruit</TD></TR><TR><TD>5cfe7d3c6deeeef08ce0444b</TD><TD>Debra Milligain</TD><TD>+1 (906) 432-2182</TD><TD>676 Merit Court, Steinhatchee, Oregon, 5491</TD><TD>banana</TD></TR></TABLE>