我想阅读2D JSON
JSON文件示例-
[
{
"name":"메뉴1",
"permission":"1",
"link":"http://naver.com"
},
{
"name":"메뉴2",
"permission":"2",
"link":"http://daum.net",
"sub":[
{
"name":"메뉴2-1",
"permission":"1",
"link":"http://google.com"
},
{
"name":"메뉴2-2",
"permission":"1",
"link":"http://yahoo.com"
}
]
}
]
我想在主菜单的底部放置一个辅助菜单。 我知道如何显示1d数组,但无法显示2d数组。 请帮助我。
<script >
$(document).ready(function() {
{
$.getJSON('./inc/menu.json', function(data) {
var html = '';
html += '<ul>';
$.each(data, function(entryIndex, entry) {
html += '<li><a href=' + entry.link + ' title=' + entry.permission + '>' + entry.name + '</a></li>';
});
html += '</ul>';
console.log(html);
$("nav").html(html);
});
return false;
}
});
</script>
答案 0 :(得分:1)
尝试一下?
$(document).ready(function() {
{
$.getJSON('./inc/menu.json', function(data) {
var html = '';
html += '<ul>';
$.each(data, function(entryIndex, entry) {
if(entry.sub && entry.sub.length > 0) {
html += '<li><ul>';
$.each(entry.sub, function(key, val) {
html += '<li><a href=' + val.link + ' title=' + val.permission + '>' + val.name + '</a></li>';
});
html += '</ul></li>';
} else {
html += '<li><a href=' + entry.link + ' title=' + entry.permission + '>' + entry.name + '</a></li>';
}
});
html += '</ul>';
console.log(html);
$("nav").html(html);
});
return false;
}
});
答案 1 :(得分:0)
您可以使用Array#map和Array#join代替$ .each。我发现它更具可读性(主观)。
还使用,解构和传播语法。
const data=[{"name":"메뉴1","permission":"1","link":"http://naver.com"},{"name":"메뉴2","permission":"2","link":"http://daum.net","sub":[{"name":"메뉴2-1","permission":"1","link":"http://google.com"},{"name":"메뉴2-2","permission":"1","link":"http://yahoo.com"}]}]
function generateHTML({link,permission,name}, sub) {
return `
<li>
<a href="${link}" title="${permission}">
${name}
</a>
${sub||""}
</li>
`
}
const res = data.map(item => {
let sub = "";
if (item.sub) {
sub = `<ul>${item.sub.map(ele=>generateHTML(ele)).join("")}</ul>`
}
return generateHTML(item, sub);
}).join("");
$("nav").html(`<ul>${res}</ul>`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav></nav>
const data = [{"name":"메뉴1","permission":"1","link":"http://naver.com"},{"name":"메뉴2","permission":"2","link":"http://daum.net","sub":[{"name":"메뉴2-1","permission":"1","link":"http://google.com"},{"name":"메뉴2-2","permission":"1","link":"http://yahoo.com","sub":[{"name":"메뉴2-2-1","permission":"1","link":"http://google.com"},{"name":"메뉴2-2-2","permission":"1","link":"http://yahoo.com"}]}]}]
function generateHTML({link,permission,name, sub}) {
return `
<li>
<a href="${link}" title="${permission}">
${name}
</a>
${sub ? `<ul>${sub.map(generateHTML).join("")}</ul>` : ""}
</li>
`
}
const res = data.map(generateHTML).join("");
$("nav").html(`<ul>${res}</ul>`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav></nav>
答案 2 :(得分:0)
尝试
const jsonArr =
[
{
"name" : "메뉴1",
"permission" : "1",
"link" : "http://naver.com"
},
{
"name" : "메뉴2",
"permission" : "2",
"link" : "http://daum.net",
"sub" :
[
{
"name" : "메뉴2-1",
"permission" : "1",
"link" : "http://google.com"
},
{
"name" : "메뉴2-2",
"permission" : "1",
"link" : "http://yahoo.com"
}
]
}
];
var html = '';
html += '<ul>';
$.each(jsonArr, function (index, value) {
html += '<li><a href=' + value.link + ' title=' + value.permission + '>' +
value.name + '</a></li>';
if (value.sub && value.sub.length > 0) {
html += '<ul>';
$.each(value.sub, function (index_sub, value_sub) {
html += '<li><a href=' + value_sub.link + ' title=' + value_sub.permission +
'>' + value_sub.name + '</a></li>';
});
html += '</ul>';
}
});
html += '</ul>';
$("nav").html(html);