【发布时间】:2021-06-23 08:22:20
【问题描述】:
我有一个 JSON 结构
{
"sections": [
{
"name": "Section 1",
"pre": "Pre text",
"post": "Post text",
"inputs": [
{
"text_01": "Evidence 01",
"text_02": "Evidence 02",
"textarea_01": "Evidence 03",
"textarea_02": "Evidence 04"
}
]
},
{
"name": "Section 2",
"pre": "Pre text lad",
"post": "Post text bro",
"inputs": [
{
"text_03": "Evidence 05",
"text_04": "Evidence 06",
"textarea_03": "Evidence 07",
"textarea_04": "Evidence 08"
}
]
}
]
}
我必须遍历的 jQuery 是:
$.getJSON( "/training/assets/json/test.json", function( data ) {
$.each( data["sections"], function( key, val ) {
console.log('[+] Top Level');
console.log(val["name"]);
console.log(val["pre"]);
console.log(val["post"]);
console.log('[+] Inputs');
$.each( val["inputs"], function( k, v ) {
$.each( val["inputs"][k], function( ke, va ) {
console.log(ke + ' - ' + va);
});
});
});
});
这会吐出以下内容,这是我想要的,但我想知道是否有更清洁的方法来实现同样的目标?
【问题讨论】:
-
我不清楚目标是什么 - 如果它只是将 JSON 的内容输出到控制台,那么为什么不直接使用
console.log(...)或console.dir(...)转储它呢?items数组是干什么用的? -
items 现已被删除 - 我希望能够动态创建表单,因此需要有名称、pre、文本字段和 textareas,然后是 post