使用$.getJSON检索JSON数组并返回值

Retrieve JSON array with $.getJSON and return values

本文关键字:数组 返回值 JSON 检索 getJSON 使用      更新时间:2023-09-26

我有一个json对象,看起来像这样。

[{
    "bannerName": "HotWire",
    "imgLocation": "img/logo/weh.jpg",
    "profiles":["1000000390","1000000391"],
    "locations":["1","250"]
},
{
    "bannerName": "The Grill",
    "imgLocation": "img/logo/weh123.jpg",
    "profiles":["1000000390","1000000391"],
    "locations":["1","250"]
}];

保存在banners.json 中

我正试图检索这些信息来创建带有引导程序的横幅。

我使用以下内容从json文件中检索信息。

$.getJSON('banners/banners.json', function(data){
  $.each(data, function(index, obj){
    $.each(obj, function(key, value){
      console.log(value);
    });
  });
});

但是,控制台中没有任何记录。从本质上讲,我需要的是将循环中的信息作为变量检索,这样我就可以将它们嵌入HTML中,但这段代码似乎无法返回任何内容。

如果有,请尝试删除json末尾的分号

[{
    "bannerName": "HotWire",
    "imgLocation": "img/logo/weh.jpg",
    "profiles":["1000000390","1000000391"],
    "locations":["1","250"]
},
{
    "bannerName": "The Grill",
    "imgLocation": "img/logo/weh123.jpg",
    "profiles":["1000000390","1000000391"],
    "locations":["1","250"]
}]

您可以在jsonint中检查您的json是否是正确的

$.getJSON('banners/banners.json', function(data){
 // try to debug what you actually getting
  console.log(data);
});