简体   繁体   English

如何使用getJSON读取和显示json二维数组?

[英]how to read and show json 2d array using getJSON?

I want to read the 2D JSON 我想阅读2D JSON

JSON file example - 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"
         }
      ]
   }
]

I want to put a secondary menu at the bottom of the primary menu. 我想在主菜单的底部放置一个辅助菜单。 I know how to show 1d array but I can't show 2d array. 我知道如何显示1d数组,但无法显示2d数组。 Please help me. 请帮我。

<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>

Try this? 尝试这个?

$(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;
  }
});

You could use Array#map and Array#join instead of $.each. 您可以使用Array#map和Array#join代替$ .each。 I find it much more readable (subjective). 我发现它更具可读性(主观)。

Also using, destructuring and spread syntax. 还使用,解构和传播语法。

 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> 

Alternative recursive version (works with any depth of sub elements): 备用递归版本(适用于任何深度的子元素):

 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> 

Try this 尝试这个

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM