Skip to content

Instantly share code, notes, and snippets.

@todbot
Created December 9, 2016 07:36
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 todbot/bc13cfa0189c95d3cf172c64168a1326 to your computer and use it in GitHub Desktop.
Save todbot/bc13cfa0189c95d3cf172c64168a1326 to your computer and use it in GitHub Desktop.
CrashSpace Sign JSON parsing in Swift
//: CrashSpace Sign JSON parsing in Swift
import Cocoa
import Foundation
let urlString = "https://crashspacela.com/sign2/json/";
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print("oh no \(error)")
} else {
print("hi there; we got server response")
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [Any]
let lastItem = parsedData[0] as! [String:Any]
print("lastItem = \(lastItem)")
let id = lastItem["id"] as! String
let msg = lastItem["msg"] as! String
let diff_mins = lastItem["diff_mins_max"] as! Int
print("lastItem: id:\(id), msg:\(msg), diff_mins:\(diff_mins)")
} catch let error as NSError {
print(error)
}
}
}.resume()
// in playground, loop forever so the task above completes
var waiting = true
while( waiting ) {
sleep(10)
print("sleeping...")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment