javascript - 我正在尝试使用 JSON 存储音乐专辑

标签 javascript arrays json object

这就是我所拥有的。我需要的是2张专辑,每张至少4首歌曲。 专辑应包括:专辑名称、艺术家姓名、发行年份。 歌曲信息应包括:歌曲名称、播放时间

歌曲应表示为对象数组。

到目前为止,这些都是我用代码编写的相册。我认为这是不正确的,但我不确定如何解决。当我通过 JSON 检查器运行它时,它给我错误

album1 = { 
 "title" : "The Avairy”,
 "artist" : "Galantis”,
 "year_released" : 2017,
 “songs”:[{
    "song1” : "Hey Alligator”,
    "songtime1” : 197,
    “song2” : “True Feeling”,
    “songtime2”: 214,
    “song3” : “Written in the scars”
    “songtime3” : 194,
    “song4” : “No Money”
    “songtime4” : 185;
    }]
}

album2 = {
    "title" : “Kolony”,
    "artist" : “Steve Aoki”,
    "year_released" : 2017,
    “songs”:[
        "song1” : “Lit”,
        "songtime1” : 150,
        “song2” : “Without you”,
        “songtime2”: 207,
        “song3” : “Been Ballin”
        “songtime3” : 180,
        “song4” : “How Else”
        “songtime4” : 152;
    ]
}

最佳答案

您的 JSON 格式存在各种问题:

  • 不能有分号

  • 对象中缺少逗号

  • 你有一些奇怪的双引号(不是常规的 ")

您可以使用例如 https://jsonlint.com/ 验证您的 json .

{
	"title": "The Avairy",
	"artist": "Galantis",
	"year_released": 2017,
	"songs": [{
		"song1": "Hey Alligator",
		"songtime1": 197,
		"song2": "True Feeling",
		"songtime2": 214,
		"song3": "Written in the scars",
		"songtime3": 194,
		"song4": "No Money",
		"songtime4": 185
	}]
}

{
	"title": "Kolony",
	"artist": "Steve Aoki",
	"year_released": 2017,
	"songs": [{
		"song1": "Lit",
		"songtime1": 150,
		"song2": "Without you",
		"songtime2": 207,
		"song3": "Been Ballin",
		"songtime3": 180,
		"song4": "How Else",
		"songtime4": 152
	}]
}

顺便说一句,我发现这种建模专辑数据的方式很奇怪。为什么不是这样的:

[{
	"title": "The Avairy",
	"artist": "Galantis",
	"year_released": 2017,
	"songs": [{
			"title": "Hey Alligator",
			"length": 197
		},
		{
			"title": "True Feeling",
			"length": 214
		},
		{
			"title": "Written in the scars",
			"length": 194
		},
		{
			"title": "No Money",
			"length": 185
		}
	]
}, {
	"title": "Kolony",
	"artist": "Steve Aoki",
	"year_released": 2017,
	"songs": [{
			"title": "Lit",
			"length": 150
		},
		{
			"title": "Without you",
			"length": 207
		},
		{
			"title": "Been Ballin",
			"length": 180
		},
		{
			"title": "How Else",
			"length": 152
		}
	]
}]

关于javascript - 我正在尝试使用 JSON 存储音乐专辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697129/

相关文章:

java - Jackson 方法中的只读属性

c# - ASP.NET 数据注释 : How to share with a JSON client?

javascript - Backbone 搜索条件

java - 数组/循环不输出主行

r - 如何在 R 中从 2D 矩阵创建 3D 数组?

javascript - PHP/JS : convert PHP array to JavaScript array

ios - 使用Swift将一些Class类型的Arraylist转换成JSON格式

javascript - 使用自定义语言 (Dust.js) 重新格式化代码?

javascript - meteor 未捕获类型错误 : Cannot read property 'push' of undefined

javascript - 搜索和过滤数组