0

我正在尝试为我正在完成的 javascript 教程设置 JSON 格式的示例数据集。

数据对象在 javascript 中如下所示:

app.Book = Backbone.Model.extend({
    defaults: {
        coverImage: 'img/getfile.jpg',
        title: 'No title',
        author: 'Unknown',
        releaseDate: 'Unknown',
        keywords: 'None'
    }
});

这是我在 .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()

但是,每当我尝试使用 GET 检索数据时,我的控制台中都会出现“对象未采用指定格式”错误。

所以我的问题是,我的 JSON 数据格式是否与 javascript 对象匹配?

谢谢!

4

1 回答 1

3

我认为您的 JSON 数组格式不正确,因为当您使用 定义模型时Backbone.Model.extend,您传入了一个对象;defaults: { ... }但是当您在 JSON 中构建 this 时,您将 this 构建为 array [],而不是 object {},我相信这是它所期望的。

示例(我已经用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"
        }
    }
]

这就是破坏您的代码的部分原因,因为键/值对不能是数组的直接成员:

"Book" : [
                    "coverImage" : "",
                    "title" : "EndersGame",
                    "author" : "OrsonScottCard",
                    "releaseDate" : "1/1/1965",
                    "keywords: "science fiction"
]

尝试在 JsonLint 中运行上述两个片段,看看会发生什么。

考虑:

如果您的服务器端代码知道它需要一个 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"
    }
];
于 2013-06-25T20:57:58.263 回答