Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Last active May 13, 2021 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artturijalli/87030c62f4c580da62097fdf51c5e066 to your computer and use it in GitHub Desktop.
Save artturijalli/87030c62f4c580da62097fdf51c5e066 to your computer and use it in GitHub Desktop.
struct Video: Decodable {
let title: String
}
let JSON = """
[{
"title": "JSON Made Simple",
"url": "www.example.com/json_data"
},
{
"title": "Learn Swift Arrays",
"url": "www.example.com/arrays_swift"
},
{
"title": "Understanding Swift Dictionaries",
"url": "www.example.com/swift_dictionaries"
}]
"""
let jsonData = JSON.data(using: .utf8)!
let videos: [Video] = try! JSONDecoder().decode([Video].self, from: jsonData)
print(videos.forEach { print($0.title) }) // -> Prints out all the video titles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment