gpt4 book ai didi

javascript - 将 JSON 数据与 JavaScript 对象匹配

转载 作者:行者123 更新时间:2023-11-28 20:26:23 24 4
gpt4 key购买 nike

我正在尝试为我正在学习的 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 对象匹配?

谢谢!

最佳答案

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

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

关于javascript - 将 JSON 数据与 JavaScript 对象匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com