Created
April 17, 2018 18:50
-
-
Save alexpaul/5a3882d307e593c1135cc21699106044 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
let string = """ | |
{"name" : "alice"} | |
{"name" : "bob"} | |
{"name" : "jane"} | |
""" | |
let stringWithCommaDelimeters = string.replacingOccurrences(of: "}", with: "},") | |
let stringWithSquareBrackets = "[\(stringWithCommaDelimeters)]" | |
let jsonData = stringWithSquareBrackets.data(using: .utf8)! | |
struct Person: Codable { | |
let name: String | |
} | |
let decoder = JSONDecoder() | |
do { | |
let people = try decoder.decode([Person].self, from: jsonData) | |
print("there are \(people.count) people") | |
} catch { | |
print("decoding error: \(error)") | |
} | |
print(jsonData) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment