Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Created April 17, 2018 18:50
Show Gist options
  • Save alexpaul/5a3882d307e593c1135cc21699106044 to your computer and use it in GitHub Desktop.
Save alexpaul/5a3882d307e593c1135cc21699106044 to your computer and use it in GitHub Desktop.
//: 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