Javascript json变量未定义

Javascript json变量未定义,javascript,jquery,json,Javascript,Jquery,Json,我在一个名为json/img_desc.json的文件夹中有一个json文件 这是json文件 { "theimages":[ { "number":1, "title":"Joy Toy teddy bear", "description":"In etc etc" } etc etc 然后我用这段代码尝试获取第一个值 $.getJSON('json/img_desc.json', function(theimages) { console.log(

我在一个名为json/img_desc.json的文件夹中有一个json文件

这是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]引用错误:未定义img_desc@ 文件:///[出于隐私考虑删除]/js/puzzle.js:246

应该是

$.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)
    })
});
文档说明它将传递一个普通对象作为第二个参数。所以,你可以这样做

$.getJSON('json/img_desc.json', function(theimages) {
    $.each(theimages.theimages, function( index, val ) {
        console.log(val, val.number);
  });
});

应该能解决你的问题。如果您有任何其他问题,请在单独的问题中提问。

使用console.log(图像编号[0]);谢谢,这很有效。但是我有另一个错误,它说我的json文件“格式不好”?这是个问题吗?第1行Json文件:{“theimages”:[complete your closing tag@GhoztIt is closed.]}是FireFox中的最后一行,如果这导致difference@Ghozt文件中似乎有语法问题。。。你能分享文件内容吗
 $.getJSON('json/img_desc.json', function(img_desc) {
    console.log(img_desc.theimages.number[0]);
});