所以我有一个JSON文件,其中包含我想要解析的信息。它看起来像这样:
{
"file_1": {
"configurations": {
"config_1": {
"config_explanation": "TYPE",
"value": "xx",
"value_explanation": "XX"
},
"config_2": {
"config_explanation": "SOME",
"value": "YY",
"value_explanation": "DOSA"
}
},
"not_important": {
.
.
},
"file_2": {
"configurations": {
"config_1": {
"config_explanation": "TYPE",
"value": "ZZ",
"value_explanation": "PP"
},
"config_2": {
"config_explanation": "SOME",
"value": "GG",
"value_explanation": "BB"
}
},
"not_important": {
.
.
}
}
因此,数据是使用多个文件创建的。 " config_X"部分是硬编码和静态的。 " not_important"部分是此时与我无关的信息。
我尝试做的是解析" config_X"中的数据。并将其添加到列表中。我遇到的问题是,如果我扫描"配置"我可以轻松地扫描file_1,添加硬编码的config_1,config_2和config_explanation,但是当我想在表格中添加它旁边的值时,我不明白我应该怎么做。这就是我做的方式,但它看起来不太好或效果不错。
$(document).ready(function() {
var requestURL = 'http://localhost:3000/xx';
var request = new XMLHttpRequest();
request.open('GET', requestURL);
request.onload = function() {
var data = JSON.parse(request.responseText);
//console.log(data);
$(parse_car_config(data)).appendTo(".list");
};
request.send();
});
//Just add the "config_x" part and "config_explanation"
function parse_car_config(data) {
var tableRowsHtml = "";
var file = 1;
for(file in data) {
$.each(data[file]['configurations'], function(section, content) {
tableRowsHtml = tableRowsHtml + "<tr>\n<td>" + section + "</td>\n" + "<td>" + content["config_explanation"] + "</td>\n" + "/tr";
});
break;
}
return tableRowsHtml;
}
应该看起来像这样:
<table>
<tbody>
<tr>
<td>config_1</td>
<td>TYPE</td>
<td>xx</td> <!-- From file_1 -->
<td>ZZ</td> <!-- From file_2 -->
</tr>
<tr>
<td>config_2</td>
<td>SOME</td>
<td>YY</td> <!-- From file_1 -->
<td>GG</td> <!-- From file_2 -->
</tr>
</tbody>
</table>
有人可以帮我解决这个问题吗? 注意:文件名,ex&#34; file_1&#34;可以更改文件名和文件数。
答案 0 :(得分:1)
也许您可以将数据格式化为以下内容:
unexpected tLABEL
a.each_with_index{|(a:, b:), i| }
^
const formatted = {};
const fileKeys = Object.keys(data);
fileKeys.forEach((file) => {
const config = data[file].configurations;
const configKeys = Object.keys(config);
configKeys.forEach((key) => {
formatted[key] = Object.assign({},
formatted[key],
{
config_explanation: config[key].config_explanation,
[file]: {
value: config[key].value,
value_explanation: config[key].value_explanation,
},
}
);
});
});
然后使用表格的格式化数据。
或者代替文件作为对象的键,您可以将它们放在如下的数组中:
const data = {
"file_1": {
"configurations": {
"config_1": {
"config_explanation": "TYPE",
"value": "xx",
"value_explanation": "XX"
},
"config_2": {
"config_explanation": "SOME",
"value": "YY",
"value_explanation": "DOSA"
},
},
},
"file_2": {
"configurations": {
"config_1": {
"config_explanation": "TYPE",
"value": "ZZ",
"value_explanation": "PP"
},
"config_2": {
"config_explanation": "SOME",
"value": "GG",
"value_explanation": "BB"
},
},
},
};
const formatted = {};
const fileKeys = Object.keys(data);
fileKeys.forEach((file) => {
const config = data[file].configurations;
const configKeys = Object.keys(config);
configKeys.forEach((key) => {
formatted[key] = Object.assign({},
formatted[key],
{
config_explanation: config[key].config_explanation,
[file]: {
value: config[key].value,
value_explanation: config[key].value_explanation,
},
}
);
});
});
console.log(formatted);
const formatted = {};
const fileKeys = Object.keys(data);
fileKeys.forEach((file) => {
const config = data[file].configurations;
const configKeys = Object.keys(config);
configKeys.forEach((key) => {
formatted[key] = Object.assign({},
formatted[key],
{
config_explanation: config[key].config_explanation,
}
);
if (!formatted[key].files) {
formatted[key].files = [];
}
formatted[key].files.push({
file,
value: config[key].value,
value_explanation: config[key].value_explanation,
});
});
});