Last active
October 28, 2015 18:34
-
-
Save speaktoalvin/e2b119a1db7625f30882 to your computer and use it in GitHub Desktop.
This file contains 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
// 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" | |
} | |
} |
This file contains 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
let json = JSON(data: data) | |
if let relatedValue = json["relationships"]["author"]["link"].related { | |
println("SwiftyJSON: \(relatedValue)") | |
} |
This file contains 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
// 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