在 Swift 中解码 JSON 时遇到问题 4
Having trouble decoding JSON in Swift 4
这是回复 JSON:
{
"feed": {
"title": "Top Albums",
"id": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json",
"author": {
"name": "iTunes Store",
"uri": "http://wwww.apple.com/us/itunes/"
},
"links": [
{
"self": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"
},
{
"alternate": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=34&popId=82&app=music"
}
],
"copyright": "Copyright © 2018 Apple Inc. All rights reserved.",
"country": "us",
"icon": "http://itunes.apple.com/favicon.ico",
"updated": "2018-07-17T01:41:38.000-07:00",
"results": [
{
"artistName": "Drake",
"id": "1405365674",
"releaseDate": "2018-06-29",
"name": "Scorpion",
"kind": "album",
"copyright": "℗ 2018 Young Money/Cash Money Records",
"artistId": "271256",
"artistUrl": "https://itunes.apple.com/us/artist/drake/271256?app=music",
"artworkUrl100": "https://is4-ssl.mzstatic.com/image/thumb/Music125/v4/5d/9b/97/5d9b97d6-9f78-e43b-7ba7-c2c42f53a166/00602567879121.rgb.jpg/200x200bb.png",
"genres": [
{
"genreId": "18",
"name": "Hip-Hop/Rap",
"url": "https://itunes.apple.com/us/genre/id18"
},
{
"genreId": "34",
"name": "Music",
"url": "https://itunes.apple.com/us/genre/id34"
}
],
"url": "https://itunes.apple.com/us/album/scorpion/1405365674?app=music"
},
...
这是我试图解码上面的代码 JSON:
struct object: Decodable {
struct feed: Decodable {
let title: String?
let id: Int?
struct Author: Decodable {
let name: String?
let uri: String?
}
let author: Author?
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]?
let copyright: String?
let country: String?
let icon: String?
let updated: String?
let results: [Album]?
}
}
struct Album: Decodable {
let artistName: String?
let id: Int?
let releaseDate: String?
let name: String?
let artworkUrl100: String?
let kind: String?
let copyright: String?
let artistId: Int?
let artistUrl: String?
struct Genre: Decodable {
let genreId: Int?
let name: String?
let url: String?
}
let genres: [Genre]?
let url: String?
}
我正在尝试获取包含专辑名称、艺术家姓名和图像的结果 url。我很难理解如何构建合适的结构来获取具有嵌套 json.
的数据
当我尝试在我的完成块中获取数据时,我最终得到了所有的零值。
//Networking
let jsonString = "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"
guard let url = URL(string: jsonString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
let obj = try JSONDecoder().decode(object.feed.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}
}.resume()
而我的结果全为零:
feed(title: nil, id: nil, author: nil, links: nil, copyright: nil, country: nil, icon: nil, updated: nil, results: nil)
你有一些问题。
首先,最外层结构是key下的一个对象feed
你需要:
struct object: Decodable {
let feed: Feed
}
struct Feed: Decodable {
let title: String
let id: String
let author: Author
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]
let copyright: String
let country: String
let icon: String
let updated: String
let results: [Album]
}
然后您将通过以下方式对其进行解码:
do {
let obj = try JSONDecoder().decode(object.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}
请注意所有 "ids" 都是 String
而不是 Int
这是回复 JSON:
{
"feed": {
"title": "Top Albums",
"id": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json",
"author": {
"name": "iTunes Store",
"uri": "http://wwww.apple.com/us/itunes/"
},
"links": [
{
"self": "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"
},
{
"alternate": "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?genreId=34&popId=82&app=music"
}
],
"copyright": "Copyright © 2018 Apple Inc. All rights reserved.",
"country": "us",
"icon": "http://itunes.apple.com/favicon.ico",
"updated": "2018-07-17T01:41:38.000-07:00",
"results": [
{
"artistName": "Drake",
"id": "1405365674",
"releaseDate": "2018-06-29",
"name": "Scorpion",
"kind": "album",
"copyright": "℗ 2018 Young Money/Cash Money Records",
"artistId": "271256",
"artistUrl": "https://itunes.apple.com/us/artist/drake/271256?app=music",
"artworkUrl100": "https://is4-ssl.mzstatic.com/image/thumb/Music125/v4/5d/9b/97/5d9b97d6-9f78-e43b-7ba7-c2c42f53a166/00602567879121.rgb.jpg/200x200bb.png",
"genres": [
{
"genreId": "18",
"name": "Hip-Hop/Rap",
"url": "https://itunes.apple.com/us/genre/id18"
},
{
"genreId": "34",
"name": "Music",
"url": "https://itunes.apple.com/us/genre/id34"
}
],
"url": "https://itunes.apple.com/us/album/scorpion/1405365674?app=music"
},
...
这是我试图解码上面的代码 JSON:
struct object: Decodable {
struct feed: Decodable {
let title: String?
let id: Int?
struct Author: Decodable {
let name: String?
let uri: String?
}
let author: Author?
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]?
let copyright: String?
let country: String?
let icon: String?
let updated: String?
let results: [Album]?
}
}
struct Album: Decodable {
let artistName: String?
let id: Int?
let releaseDate: String?
let name: String?
let artworkUrl100: String?
let kind: String?
let copyright: String?
let artistId: Int?
let artistUrl: String?
struct Genre: Decodable {
let genreId: Int?
let name: String?
let url: String?
}
let genres: [Genre]?
let url: String?
}
我正在尝试获取包含专辑名称、艺术家姓名和图像的结果 url。我很难理解如何构建合适的结构来获取具有嵌套 json.
的数据当我尝试在我的完成块中获取数据时,我最终得到了所有的零值。
//Networking
let jsonString = "https://rss.itunes.apple.com/api/v1/us/apple-music/top-albums/all/25/explicit.json"
guard let url = URL(string: jsonString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
let obj = try JSONDecoder().decode(object.feed.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}
}.resume()
而我的结果全为零:
feed(title: nil, id: nil, author: nil, links: nil, copyright: nil, country: nil, icon: nil, updated: nil, results: nil)
你有一些问题。
首先,最外层结构是key下的一个对象feed
你需要:
struct object: Decodable {
let feed: Feed
}
struct Feed: Decodable {
let title: String
let id: String
let author: Author
struct Link: Decodable {
let url: String?
private enum CodingKeys: String, CodingKey {
case url = "self"
}
}
let links: [Link]
let copyright: String
let country: String
let icon: String
let updated: String
let results: [Album]
}
然后您将通过以下方式对其进行解码:
do {
let obj = try JSONDecoder().decode(object.self, from: data)
print(obj)
} catch let jsonError {
print("Error with json", jsonError)
}
请注意所有 "ids" 都是 String
而不是 Int