试图访问JSON对象的数组的子节点:未捕获的TypeError:无法读取未定义的属性“长度”

时间:2014-12-15 19:13:22

标签: javascript jquery ajax json

我正在尝试从JSON对象获取Description数组,将其循环并将其返回给用户。

我的JSON代码格式如下:

[
   {
      "FoodNumber":"4567421",
      "FoodName":"Banana",
      "FoodCode":"110",
      "DocumentDate":"19991223",
      "ArrivalDate":"20020820",
      "BogusField":"1241231",
      "WhereFound":"In the jungle, man",
      "DateEaten":"I dunno, every day?",
      "Description":
      [
         "A vicious predator, the ravenous Banana stalks the jungles forcing monkeys to eat it",
         "Monkeys think it's food, but it's really a parasite"
      ]
   }
]

这是在ajax success: function (z)内部。我使用var json = $.parseJSON(z.d);解析它,这使我可以访问除Description之外的每个单独的数据类型。我需要能够遍历该文本数组。我目前正在使用这个jQuery代码,它适用于除Description之外的所有代码:

var json = $.parseJSON(z.d);
$.each(json[0], function (i, obj) {
    // obj.FoodName gives us the FoodName string, etc.
    // ... lots of irrelevant code here

    $.each(obj.Description, function (j, t) {
        console.log(t);
    });

    // ... more code here.
});

当我为Description数组使用该循环代码时,出现此错误:Uncaught TypeError: Cannot read property 'length' of undefined.

我做错了什么?任何帮助将不胜感激。

谢谢!

0 个答案:

没有答案