Skip to content

Instantly share code, notes, and snippets.

@anoop4real
Created July 30, 2018 06:10
Show Gist options
  • Select an option

  • Save anoop4real/604e55718d54b4e58056911652229abb to your computer and use it in GitHub Desktop.

Select an option

Save anoop4real/604e55718d54b4e58056911652229abb to your computer and use it in GitHub Desktop.
A sample to decode a JSON structure using Codable
import Foundation
import UIKit
let json = """
[
{
"title": "Taylor Swift",
"artist": "Taylor Swift",
"url": "https://www.amazon.com/Taylor-Swift/dp/B0014I4KH6",
"image": "https://images-na.ssl-images-amazon.com/images/I/61McsadO1OL.jpg",
"thumbnail_image": "https://i.imgur.com/K3KJ3w4h.jpg"
},
{
"title": "Fearless",
"artist": "Taylor Swift",
"url": "https://www.amazon.com/Fearless-Enhanced-Taylor-Swift/dp/B001EYGOEM",
"image": "https://images-na.ssl-images-amazon.com/images/I/51qmhXWZBxL.jpg",
"thumbnail_image": "https://i.imgur.com/K3KJ3w4h.jpg"
},
{
"title": "Speak Now",
"artist": "Taylor Swift",
"url": "https://www.amazon.com/Speak-Now-Taylor-Swift/dp/B003WTE886",
"image": "https://images-na.ssl-images-amazon.com/images/I/51vlGuX7%2BFL.jpg",
"thumbnail_image": "https://i.imgur.com/K3KJ3w4h.jpg"
},
{
"title": "Red",
"artist": "Taylor Swift",
"url": "https://www.amazon.com/Red-Taylor-Swift/dp/B008XNZMOU",
"image": "https://images-na.ssl-images-amazon.com/images/I/41j7-7yboXL.jpg",
"thumbnail_image": "https://i.imgur.com/K3KJ3w4h.jpg"
},
{
"title": "1989",
"artist": "Taylor Swift",
"url": "https://www.amazon.com/1989-Taylor-Swift/dp/B00MRHANNI",
"image": "https://images-na.ssl-images-amazon.com/images/I/717DWgRftmL._SX522_.jpg",
"thumbnail_image": "https://i.imgur.com/K3KJ3w4h.jpg"
}
]
"""
enum JSONErrors:Error{
case ParseError
}
struct MusicAlbums: Codable{
let albumTitle: String?
let albumArtist : String?
let albumURL: String?
let albumImage : String?
let albumThumbnailImage : String?
enum CodingKeys: String, CodingKey{
case albumTitle = "title"
case albumArtist = "artist"
case albumURL = "url"
case albumImage = "image"
case albumThumbnailImage = "thumbnail_image"
}
// init (from decoder :Decoder) throws {
// do {
//
// let container = try decoder.container(keyedBy: CodingKeys.self)
// albumTitle = try container.decode(String.self, forKey: .albumTitle)
// albumArtist = try container.decode(String.self, forKey: .albumArtist)
// albumURL = try container.decode(String.self, forKey: .albumURL)
// albumImage = try container.decode(String.self, forKey: .albumImage)
// albumThumbnailImage = try container.decode(String.self, forKey: .albumThumbnailImage)
// } catch {
// throw JSONErrors.ParseError
// }
//
// }
init (from decoder :Decoder) throws {
do {
let container = try decoder.container(keyedBy: CodingKeys.self)
albumTitle = try container.decodeIfPresent(String.self, forKey: .albumTitle)
albumArtist = try container.decodeIfPresent(String.self, forKey: .albumArtist)
albumURL = try container.decodeIfPresent(String.self, forKey: .albumURL)
albumImage = try container.decodeIfPresent(String.self, forKey: .albumImage)
albumThumbnailImage = try container.decodeIfPresent(String.self, forKey: .albumThumbnailImage)
} catch {
throw JSONErrors.ParseError
}
}
}
if let jsonData = json.data(using: .utf8)
{
let jsonObject = try? JSONDecoder().decode([MusicAlbums].self, from: jsonData)
print(jsonObject)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment