2013-10-14 52 views
0

我有一个名为JSON文件夹中的一个JSON文件/ img_desc.jsonJSON变量未定义

这里是JSON文件

{ "theimages":[ 
    { 
    "number":1, 
    "title":"Joy Toy teddy bear", 
    "description":"In etc etc" 
    } etc etc 

然后我用这个代码,以尝试并获得的第一个值。

$.getJSON('json/img_desc.json', function(theimages) { 
    console.log(img_desc.theimages.number[0]); 
}); 

错误

它说这

[15:06:46.951] ReferenceError: img_desc is not defined @ file:///[removed for privacy]/js/puzzle.js:246

+1

使用的console.log(theimages.number [0]); –

回答

5

它应该是

$.getJSON('json/img_desc.json', function (theimages) { 
    console.log(theimages.theimages[0].number); 

    //if you want to loop 
    $.each(theimages.theimages, function (idx, obj) { 
     console.log(obj.number) 
    }) 
}); 
+0

谢谢,这工作。但是,我有另一个错误,它说我的JSON文件是“不正常”?这是一个问题吗? 1号线JSON文件:{“theimages”: – Ghozt

+1

完成关闭标签@Ghozt – Olrac

+0

它关闭。 ]}是FireFox中的最后一行 – Ghozt

1
$.getJSON('json/img_desc.json', function(img_desc) { 
    console.log(img_desc.theimages.number[0]); 
}); 

应该解决您的问题。就好像您还有其他问题一样,请在单独的问题中提出。