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

时间:2017-10-11 20:38:42

标签: 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;
    ]
}

3 个答案:

答案 0 :(得分:1)

您的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
	}]
}

BTW我觉得这种建模专辑数据的方式很奇怪。为什么不这样:

[{
	"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
		}
	]
}]

答案 1 :(得分:0)

你应该在JSON中的任何地方使用" 而不是,除非它在字符串中是这样的:

json = {
    "title": "Title is “Without you”",
    "artist": "“Steve Aoki”"
};

同样在专辑2中你有"歌曲"声明为数组,但您正在使用它像对象。在json中你也有分号,这是错误的,并且在变量声明和缺少coma之后缺少分号。正确的代码看起来应该是这样的(我只给予album1,自己做专辑2来学习一些东西):

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
    }]
};

如果你想成为一名程序员,你需要非常精准。你甚至不会用很多语法错误写出简单的函数。

答案 2 :(得分:0)

一组对象表明不止一个。你想要这个结构:

{
  "title" : "Kolony",
  "artist" : "Steve Aoki",
  "year_released" : 2017,
  "songs":[
    { 
      "song" : "Lit",
      "songtime" : 150
    },
    { 
      "song" : "Without you",
      "songtime" : 270
    },
    { 
      "song" : "Been Ballin",
      "songtime" : 180
    },
    { 
      "song" : "How Else",
      "songtime" : 152
    }
  ]
}

我还注意到不应该存在的分号和混合引号。而且你需要始终使用“而不是”或“。