Skip to content

Instantly share code, notes, and snippets.

@speaktoalvin
Last active October 28, 2015 18:34
Show Gist options
  • Save speaktoalvin/e2b119a1db7625f30882 to your computer and use it in GitHub Desktop.
Save speaktoalvin/e2b119a1db7625f30882 to your computer and use it in GitHub Desktop.
// Example JSON
{
"type": "articles",
"id": "1",
"attributes": {
"title": "Rails is Omakase"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "http://example.com/articles/1"
}
}
let json = JSON(data: data)
if let relatedValue = json["relationships"]["author"]["link"].related {
println("SwiftyJSON: \(relatedValue)")
}
// Traditional Parsing
func requestData(link : String)
{
do {
let data = try String(contentsOfURL: NSURL(string: link)!, encoding: NSASCIIStringEncoding)
if let properData = data.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true) {
let json : AnyObject? = try NSJSONSerialization.JSONObjectWithData(properData, options: [])
self.getValues(json)
}
}
catch {
}
}
func getValues( object : AnyObject?)
{
if let json = object, dataDictionary = json["data"] as? Dictionary<String, String>, type = dataDictionary["data"], id = dataDictionary["id"] {
// JSON
print("\(type) || \(id)")
} else {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment