我试图解析一些JSON数据,但遇到了一些问题。这是JSON:
{
"result": {
"artist": {
"name": "The Beatles"
},
"track": {
"name": "Yesterday",
"text": "[Verse 1]\nYesterday\nAll my troubles seemed so far away\nNow it looks as though they're here to stay\nOh, I believe in yesterday\n\n[Verse 2]\nSuddenly\nI'm not half the man I used to be\nThere's a shadow hanging over me\nOh, yesterday came suddenly\n\n[Chorus]\nWhy she had to go\nI don't know, she wouldn't say\nI said something wrong\nNow I long for yesterday\n\n[Verse 3]\nYesterday\nLove was such an easy game to play\nNow I need a place to hide away\nOh, I believe in yesterday\n\n[Chorus]\nWhy she had to go\nI don't know, she wouldn't say\nI said something wrong\nNow I long for yesterday\n\n[Verse 4]\nYesterday\nLove was such an easy game to play\nNow I need a place to hide away\nOh, I believe in yesterday",
"lang": {
"code": "en",
"name": "English"
}
},
"copyright": {
"notice": "Yesterday lyrics are property and copyright of their owners. Commercial use is not allowed.",
"artist": "Copyright The Beatles",
"text": "All lyrics provided for educational purposes and personal use only."
},
"probability": "75.00",
"similarity": 1
}
}
到目前为止,我的代码是:
guard let url = URL(string: "https://orion.apiseeds.com/api/music/lyric/Beatles\Yesterday?apikey=xxx") else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
}
guard let data = data else { return }
do {
let data = try JSONDecoder().decode(Result.self, from: data)
print(data)
} catch let jsonError {
print(jsonError)
}
}.resume()
struct Result: Codable {
let artist: [Artist]
}
struct Artist: Codable {
let name: String
}
当我尝试运行它时得到的错误是:
keynotfound(编码键(stringvalue:“artist”,intvalue:nil),
swift.decodingError.context(编码路径:[],debugDescription:“否”
与键编码键关联的值(StringValue:\“artist\”,
intvalue:nil)(“艺术家\”)“,underlyingError:nil)
我只想从歌曲中得到歌词。
请有人看看这个,因为我正在努力很多。