简体   繁体   English

尝试访问json数组中的嵌套项目

[英]Trying to access nested items in an json array

I have a javascript object taken from a json file. 我有一个从json文件中获取的javascript对象。 The problem is I am trying to access items in a nested array that parent level is called "menu". 问题是我试图访问父级称为“菜单”的嵌套数组中的项目。 I need to retrieve the id, description, content and cssClass of this sub menu object. 我需要检索此子菜单对象的ID,描述,内容和cssClass。 I am getting the following error Uncaught TypeError: Cannot read property 'length' of null Here is my code. 我收到以下错误Uncaught TypeError:无法读取null的属性“ length”,这是我的代码。

The HTML for page 页面的HTML

-------------------------

The Javascript Object and Jquery Javascript对象和Jquery

--------------------------------
//the json code
var object = {
"menuItems":{
"menu":[
  {
     "id":"contact",
     "leaf":true,
     "description":"Contact Us",
     "link":"",
     "content":"contactUs.html",
     "cssClass":"static-content",
     "menu":null
  },
  {
     "id":"rules",
     "leaf":false,
     "description":"Sports Betting Rules",
     "link":"",
     "content":"",
     "cssClass":"",
     "menu":[
        {
           "id":"types",
           "leaf":true,
           "description":"Wager Types",
           "link":"",
           "content":"wagerTypes.html",
           "cssClass":"static-content wager-types",
           "menu":null
        },
        {
           "id":"odds",
           "leaf":true,
           "description":"Odds & Lines",
           "link":"",
           "content":"oddsAndLines.html",
           "cssClass":"static-content",
           "menu":null
        },
        {
           "id":"policies",
           "leaf":true,
           "description":"Rules & Policies",
           "link":"",
           "content":"rulesAndPolicies.html",
           "cssClass":"static-content rules-policies",
           "menu":null
        },
        {
           "id":"bonuses",
           "leaf":true,
           "description":"Sports Bonuses",
           "link":"",
           "content":"sportsBonuses.html",
           "cssClass":"static-content",
           "menu":null
        }
     ]
  },
  {
     "id":"conditions",
     "leaf":false,
     "description":"Terms & Conditions",
     "link":"",
     "content":"",
     "cssClass":"",
     "menu":[
        {
           "id":"termsOfService",
           "leaf":true,
           "description":"Terms of Service",
           "link":"",
           "content":"termsOfService.html",
           "cssClass":"static-content",
           "menu":null
        },
        {
           "id":"privacy",
           "leaf":true,
           "description":"Privacy Policy",
           "link":"",
           "content":"privacy.html",
           "cssClass":"static-content",
           "menu":null
        }
     ]
  },
  {
     "id":"view",
     "leaf":true,
     "description":"View in: Mobile | Full Site",
     "link":"",
     "content":"view.html",
     "cssClass":"static-content",
     "menu":null
   }
   ]
}
};
/*---- end of object----*/   
/*-----javascript functions----*/  
$('#menuItems').append('<ul/>')
$.each(object.menuItems.menu, function() {
    var list = $('#menuItems ul'),
    listItem = $('<li/>');
$.each(this.menu, function() {  
  listItem.append($(this.id).text(this.description).text(this.content).text(this.cssClass).text);
});

list.append(html)
// end of function
});

<!-- end of code -->

Can someone please help with this as I have looked online for the simple example but you can never find simple things online. 有人可以帮忙吗,因为我在网上查找了简单的示例,但您永远无法在网上找到简单的东西。

Your problem is here: 您的问题在这里:

listItem.append($(this.id)...

Your json has like "id" : "view", while to select an element by ID you also need # sign, so just change above to: 您的json就像“ id”:“ view”,而要通过ID选择元素时,您还需要#号,因此只需将上面的内容更改为:

listItem.append($('#' + this.id)...
$('#menuItems').append('<ul/>')
$.each(object.menuItems.menu, function() {
    var list = $('#menuItems ul'),
    listItem = $('<li/>');
// check for null
if (this.menu)
$.each(this.menu, function() {  
  listItem.append($(this.id).text(this.description).text(this.content).text(this.cssClass).text);
});

list.append(html)
// end of function
});

Your problem is that your are trying to access a potentially empty menu item (it's the case in your first object). 您的问题是您正在尝试访问可能为空的菜单项(第一个对象就是这种情况)。 You must test for that case before your second $.each and either continue by returning true or do something else. 您必须在第二个$.each之前测试该情况,然后继续返回true或执行其他操作。

PS : you also have a problem on this line : PS:您在这条线上也有问题:

listItem.append($(this.id).text(this.description).text(this.content).text(this.cssClass).text);

each time you call the text function, it will replace completly what is in your html element 每次调用text函数时,它将完全替换html元素中的内容

PS2 : Also, the last .text will return the content of the jquery text function, probably not what you had in mind ... PS2:此外,最后一个.text将返回jquery文本函数的内容,可能不是您所想的...

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

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