javascript - 我该如何循环这个 JSON 数据
问题描述
我有想要显示为 HTML 表格的 JSON 数据:
{
"foo": {
"col-1": "foo",
"col-2": "faa",
"col-3": "foobar"
},
"faa": {
"col-1": "foo",
"col-2": "faa",
"col-3": "foobar"
}
}
到目前为止,我尝试简单地res.length
使用循环进行迭代while
,但res.length
返回为undefined以及 usingJSON.parse
和for ... in
循环。都没有奏效。
到目前为止我的代码:
const startingNode = document.querySelector("#appendTable");
const table = document.createElement("table");
const tbody = document.createElement("tbody");
const req = new XMLHttpRequest();
const reqURL = "http://localhost:8000/dict_en.json";
req.open("GET", reqURL);
req.responseType = "json";
req.send();
req.onload = () => {
const res = JSON.parse(req.response);
console.log(res.length);
let i = 0;
while (i < res.length) {
console.log(i);
i++;
}
}
table.innerHTML = "<thead><tr><td>Foo<td>Faa</td><td>Foobar</td></td></tr></thead>";
table.appendChild(tbody);
startingNode.appendChild(table);
解决方案
推荐阅读
- apache - 重写 apache 写入规则
- javascript - Jquery在点击时选择表格行不起作用
- mocha.js - 我可以在 mocha awesome html 报告中添加柏树截图吗
- prolog - Prolog中术语的统一
- spring-aop - 这个切入点在 Spring AOP 中是如何工作的?
- c# - 从 WPF 数据网格中当前选定的行获取值的最佳方法是什么?
- pytorch - TypeError:“NoneType”对象在 pytorch 中的 loss.backward 中不可迭代
- react-native - Expo 的 SecureStore 能持续多久?
- python-3.x - 如何使用 BS4 获取 html 正文的特定部分
- ffmpeg - 转换为 HLS VS MPEGTS