【问题标题】:I am trying to store a music album using JSON我正在尝试使用 JSON 存储音乐专辑
【发布时间】:2018-03-23 15:42:46
【问题描述】:

这就是我所拥有的。我需要的是 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;
    ]
}

【问题讨论】:

  • 有什么错误?我发现您使用了错误的报价类型。
  • 错误:第 1 行解析错误:album1 = { "title": 这是我一开始就遇到的错误
  • 同上,不正确的引号,并且你的歌曲属性是一个数组,包含第一张专辑中的单个对象,第二张专辑中有语法错误
  • 完整的错误应该表明出了什么问题
  • 我现在很困惑。那么歌曲名称应该是放入数组中的那些?

标签: javascript arrays json object


【解决方案1】:

一组对象暗示不止一个。你想要这个结构:

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

我还注意到不应该出现的分号和混淆的引号。而且您需要始终使用“而不是”或“。

【讨论】:

    【解决方案2】:

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

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

    同样在专辑 2 中,您将“歌曲”声明为数组,但您将其用作对象。您在 json 中也有分号,这是错误的,并且在变量声明和缺少逗号后缺少分号。正确的代码应该是这样的(我只给album1,自己做album2来学习):

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

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

    【讨论】:

    • 在这种情况下,专辑名称是 kolony。那么它应该看起来像这样吗? json = { "title": "Title is "Kolony"", "artist": ""Steve Aoki", "year": 2017 };
    • 这只是一个例子。不要明确地接受所有内容:)
    【解决方案3】:

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

    【讨论】:

    • 好吧,我们都从某个地方开始。以这种方式看待它比我拥有它的方式更有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2021-06-21
    相关资源
    最近更新 更多