我正在尝试确定这里的问题。基本上,我正在将带有JSON内容的文件加载到backbone.js集合中。它正确地定位文件,加载内容,我可以看到(通过浏览器开发人员工具)12个JSON对象中的一个是可访问的,但它缺少其他11个对象,并且看起来加载的是被归类为“属性”。我对backbone.js有些新意,我不确定我做错了什么。代码和截图如下:
我用来从文件中加载JSON内容的代码:
var cmd = Backbone.Model.extend({});
var cmdList = Backbone.Collection.extend({
model: cmd
});
var myCmds = new cmdList();
myCmds.fetch({
add: true,
url: "CustomFiles/Commands.txt",
success: function () {
console.log("JSON file load was successful", myCmds);
$.each(myCmds, function (key, value) {
console.log(key + ": " + value.Description);
});
},
error: function () {
console.log("There was some error in loading and processing the JSON file");
}
});
以下是包含JSON内容的Commands.txt文件的缩写版本:
{
"Command": {
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 1,
"Name": "Start Services",
"Description": "To start all Services",
"Type": 1,
"Data": ""
},
"Command": {
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 2,
"Name": "Stop Services",
"Description": "To stop all Services",
"Type": 2,
"Data": ""
},
[9 other JSON objects (commands) - formatted exactly the same way...],
"Command": {
"build": 1,
"TouchedBy": "corpUser2",
"TouchedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"CreatedBy": "corpUser2",
"CreatedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 28,
"Name": "Stop Services T",
"Description": "To stop all Services for T",
"Type": 2,
"Data": ""
}
}
我的JSON内容似乎是正确的,但正如您从开发人员工具的屏幕截图中看到的那样,它正在加载,但它没有加载所有对象,我无法访问“描述”的值'命令'对象的属性。
在下面的屏幕截图中,您可以看到只加载了一个命令,并将其标识为属性。我是否过度分析/过度复杂,或者有什么简单的我可以忽略?
如何正确加载和迭代这些JSON对象?
更新:
我修改了Commands.txt文件,以便不再出现“Command”键,并更改了开始和结束括号,以便对象成为数组的一部分。所以,我离开了这个:
{
"Command": {
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 1,
"Name": "Start Services",
"Description": "To start all Services",
"Type": 1,
"Data": ""
},
"Command": {
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 2,
"Name": "Stop Services",
"Description": "To stop all Services",
"Type": 2,
"Data": ""
},
[9 other JSON objects (commands) - formatted exactly the same way...],
"Command": {
"build": 1,
"TouchedBy": "corpUser2",
"TouchedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"CreatedBy": "corpUser2",
"CreatedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 28,
"Name": "Stop Services T",
"Description": "To stop all Services for T",
"Type": 2,
"Data": ""
}
}
到此:
[{
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5130000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 1,
"Name": "Start Services",
"Description": "To start all Services",
"Type": 1,
"Data": ""
},
{
"build": 1,
"TouchedBy": "corpUser1",
"TouchedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"CreatedBy": "corpUser1",
"CreatedWhenUTC": "2013-10-24 06:58:15.5170000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 2,
"Name": "Stop Services",
"Description": "To stop all Services",
"Type": 2,
"Data": ""
},
[9 other JSON objects (commands) - formatted exactly the same way...],
{
"build": 1,
"TouchedBy": "corpUser2",
"TouchedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"CreatedBy": "corpUser2",
"CreatedWhenUTC": "2014-10-30 08:48:37.0770000 +00:00",
"Active": "TRUE",
"MSRowversion": "",
"id": 28,
"Name": "Stop Services T",
"Description": "To stop all Services for T",
"Type": 2,
"Data": ""
}]
这解决了我的大部分问题。我仍然有一个问题,访问该命令的一个属性值,但我会做的工作尝试找出那个部分,如果我需要,可以回来一个新的问题。
答案 0 :(得分:0)
您的对象中有三个具有相同名称(Command
)的键。每个键都将覆盖最后一个键,这解释了为什么只加载了一个嵌套对象。