Jquery 一个json文件中包含多个对象

Jquery 一个json文件中包含多个对象,jquery,json,Jquery,Json,我正在开发一个映射工具,并使用jQuery从服务器下载JSON数据。jquery的代码当前为: $.getJSON("start.json", function(data) { var items = []; $.each(data, function(key, val) { alert("data: " + key + "->" + val); }); }); json文件如下所示: { "type" : "obstacle",

我正在开发一个映射工具,并使用jQuery从服务器下载JSON数据。jquery的代码当前为:

$.getJSON("start.json", function(data) {
    var items = [];

    $.each(data, function(key, val) {
        alert("data: " + key + "->" + val);
    });
});
json文件如下所示:

{
    "type"  :   "obstacle",
    "top"   :   20,
    "left"  :   10,
    "height":   50,
    "width" :   70,
    "tall"  :   10
}
[
    { 
        "type"  :   "obstacle", 
        "top"   :   20, 
        "left"  :   10, 
        "height":   50, 
        "width" :   70, 
        "tall"  :   10 
    } ,
    { 
        "type"  :   "obstacle", 
        "top"   :   50, 
        "left"  :   10, 
        "height":   50, 
        "width" :   20, 
        "tall"  :   10 
    } 
]
现在,“问题”是,可以清楚地看到,在这种格式中,json只能包含一个对象。我必须如何构造我的文件(以及随后的jquery)以拥有多个对象


提前感谢。

您可以使用阵列存储它们:

{
  "objects": [
    {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }, {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }, {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }
    ]
}
现在,当您迭代时,可以访问单个对象的属性:

$.each(data.objects, function(key, val) {
  console.log(key + ' => ' + val);
});

您可以使用数组来存储它们:

{
  "objects": [
    {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }, {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }, {
      "type"  :   "obstacle",
        "top"   :   20,
        "left"  :   10,
        "height":   50,
        "width" :   70,
        "tall"  :   10
      }
    ]
}
现在,当您迭代时,可以访问单个对象的属性:

$.each(data.objects, function(key, val) {
  console.log(key + ' => ' + val);
});

您可以创建一个JSON对象数组,如下所示:

{
    "type"  :   "obstacle",
    "top"   :   20,
    "left"  :   10,
    "height":   50,
    "width" :   70,
    "tall"  :   10
}
[
    { 
        "type"  :   "obstacle", 
        "top"   :   20, 
        "left"  :   10, 
        "height":   50, 
        "width" :   70, 
        "tall"  :   10 
    } ,
    { 
        "type"  :   "obstacle", 
        "top"   :   50, 
        "left"  :   10, 
        "height":   50, 
        "width" :   20, 
        "tall"  :   10 
    } 
]
Edit:jQuery类似于:

$.each(data, function(index, obstacle))
{
    $.each(obstacle, function(key, val) { 
        alert("data: " + key + "->" + val); 
    }); 
}

您可以创建一个JSON对象数组,如下所示:

{
    "type"  :   "obstacle",
    "top"   :   20,
    "left"  :   10,
    "height":   50,
    "width" :   70,
    "tall"  :   10
}
[
    { 
        "type"  :   "obstacle", 
        "top"   :   20, 
        "left"  :   10, 
        "height":   50, 
        "width" :   70, 
        "tall"  :   10 
    } ,
    { 
        "type"  :   "obstacle", 
        "top"   :   50, 
        "left"  :   10, 
        "height":   50, 
        "width" :   20, 
        "tall"  :   10 
    } 
]
Edit:jQuery类似于:

$.each(data, function(index, obstacle))
{
    $.each(obstacle, function(key, val) { 
        alert("data: " + key + "->" + val); 
    }); 
}

伟大的那么jquery呢?
data.objects
将是一个存储所有对象的数组。迭代它,然后
console.log()
一些东西。太棒了!那么jquery呢?
data.objects
将是一个存储所有对象的数组。在它上面迭代并使用
console.log()
一些东西。我收到了一个警报:
data:0->[object object]
我收到了一个警报:
data:0->[object object]