[英]matching JSON data to javascript object
I am trying to setup a sample set of data in JSON format for a javascript tutorial I am working through. 我正在尝试为我正在处理的JavaScript教程设置JSON格式的数据示例集。
The data object looks like this in javascript: 数据对象在javascript中如下所示:
app.Book = Backbone.Model.extend({
defaults: {
coverImage: 'img/getfile.jpg',
title: 'No title',
author: 'Unknown',
releaseDate: 'Unknown',
keywords: 'None'
}
});
And here is the data JSON sample I am creating in my .net server that I will be retrieving to populate the object above. 这是我在.net服务器中创建的数据JSON示例,我将在其中填充上面的对象。
Private Shared Books As String =
<test>
[
{
"Book" : [
"coverImage" : "",
"title" : "Enders Game",
"author" : "Orson Scott Card",
"releaseDate" : "1/1/1965",
"keywords: "science fiction"
]
}
],
[
{
"Book" : [
"coverImage" : "",
"title" : "Parable of the Sower",
"author" : "Octavia E. Butler",
"releaseDate" : "1/1/2000",
"keywords: "science fiction"
]
}
],
[
{
"Book" : [
"coverImage" : "",
"title" : "Fahrenheit 451: A Novel",
"author" : "Ray Bradbury",
"releaseDate" : "1/1/1950",
"keywords: "science fiction"
]
}
]
</test>.Value.Trim()
However, whenever I try to retreive the data using GET, I get an 'object not in specified format' error in my console. 但是,每当尝试使用GET检索数据时,控制台中都会出现“对象未采用指定格式”错误。
So my question is, does my JSON data format match up to the javascript object? 所以我的问题是,我的JSON数据格式与javascript对象匹配吗?
Thanks! 谢谢!
I think your JSON array is in an incorrect format because, when you define the model using Backbone.Model.extend
, you pass in an object; 我认为您的JSON数组格式不正确,因为使用
Backbone.Model.extend
定义模型时,您传入一个对象; defaults: { ... }
but when you are constructing this in your JSON, you are constructing this as an array []
, not an object {}
, which I believe is what it is expecting. defaults: { ... }
但是当您在JSON中构造它时,您将其构造为一个数组[]
,而不是一个对象{}
,我相信这是期望的。
Example (I've tested this with JsonLint ) 示例(我已经用JsonLint测试过)
[ // This is the syntax notation for an array in JSON / JavaScript.
{ // An array can only hold objects (not key/value pairs), so lets wrap each instance of "Book" as an object.
"Book": { // Book can now be defined as a set of key/value pairs.
"coverImage": "",
"title": "Enders Game",
"author": "Orson Scott Card",
"releaseDate": "1/1/1965",
"keywords": "sciencefiction"
}
},
{
"Book": {
"coverImage": "",
"title": "Fahrenheit 451: A Novel",
"author": "Ray Bradbury",
"releaseDate": "1/1/1950",
"keywords": "sciencefiction"
}
},
{
"Book": {
"coverImage": "",
"title": "ParableoftheSower",
"author": "OctaviaE.Butler",
"releaseDate": "1/1/2000",
"keywords": "science fiction"
}
}
]
This is what is breaking your code, in part, because, key/value pairs cannot be direct members of an array: 这就是破坏代码的部分原因,因为键/值对不能是数组的直接成员:
"Book" : [
"coverImage" : "",
"title" : "EndersGame",
"author" : "OrsonScottCard",
"releaseDate" : "1/1/1965",
"keywords: "science fiction"
]
Try run the above two snippets in JsonLint and see what happens. 尝试在JsonLint中运行以上两个片段,看看会发生什么。
Consideration: 考虑:
If you server-side code knows that it is expecting an array of type Book, you can actually simplify this JSON, like so: 如果您的服务器端代码知道期望使用Book类型的数组,则可以实际简化此JSON,如下所示:
[
{ // Your code knows it's expecting "Book", so lets just add the properties of each "Book" here...
// You don't always need to be explicit with your naming of objects as often, the server will know how to handle this.
"coverImage": "",
"title": "Enders Game",
"author": "Orson Scott Card",
"releaseDate": "1/1/1965",
"keywords": "sciencefiction"
},
{
"coverImage": "",
"title": "Fahrenheit 451: A Novel",
"author": "Ray Bradbury",
"releaseDate": "1/1/1950",
"keywords": "sciencefiction"
},
{
"coverImage" : "",
"title" : "ParableoftheSower",
"author" : "OctaviaE.Butler",
"releaseDate" : "1/1/2000",
"keywords": "science fiction"
}
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.