Skip to content

Instantly share code, notes, and snippets.

@alskipp
Last active August 29, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alskipp/b4e1852d6375ff13c110 to your computer and use it in GitHub Desktop.
Save alskipp/b4e1852d6375ff13c110 to your computer and use it in GitHub Desktop.
iTunes JSON parsing example in Swift
{
"resultCount":1,
"results": [
{"isGameCenterEnabled":false,
"screenshotUrls":["http://a4.mzstatic.com/us/r30/Purple7/v4/8d/94/3a/8d943ac7-77de-2011-d920-ccf1c0eb0cd2/screen1136x1136.jpeg", "http://a4.mzstatic.com/us/r30/Purple7/v4/ca/cd/1c/cacd1ca9-987c-9ad5-1861-67a9ff0653e7/screen1136x1136.jpeg", "http://a5.mzstatic.com/us/r30/Purple7/v4/30/26/af/3026af60-9efd-771c-8a42-5183fdc74df2/screen1136x1136.jpeg", "http://a5.mzstatic.com/us/r30/Purple7/v4/d6/4d/2f/d64d2f39-da72-cc1e-35bd-f52623d1a5a0/screen1136x1136.jpeg"],
"ipadScreenshotUrls":["http://a5.mzstatic.com/us/r30/Purple5/v4/ad/23/12/ad2312c5-a5b1-bc75-3e84-523981013388/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple1/v4/14/6b/cd/146bcde2-8461-604b-0722-f8744b28dac9/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple5/v4/c5/a1/48/c5a14861-13fb-18bb-0752-e0d13b4d2c3c/screen480x480.jpeg", "http://a4.mzstatic.com/us/r30/Purple7/v4/c2/36/25/c2362536-f6fc-4ef6-2a03-9b899ca00af9/screen480x480.jpeg"], "artworkUrl60":"http://is5.mzstatic.com/image/pf/us/r30/Purple1/v4/32/2f/d8/322fd80e-c177-483b-9662-0261aadc1232/AppIcon60x60_U00402x.png", "artworkUrl512":"http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/85/2d/45/852d4578-89ba-8db4-52fa-b93fa95ebfc6/mzl.coqjmrmb.png", "artistViewUrl":"https://itunes.apple.com/us/artist/yelp/id284910353?uo=4", "kind":"software", "features":["iosUniversal"],
"supportedDevices":["iPhone5", "iPadMini", "iPhone5c", "iPadThirdGen", "iPadThirdGen4G", "iPadMini4G", "iPhone6", "iPhone4S", "iPadFourthGen4G", "iPad2Wifi", "iPadFourthGen", "iPodTouchFifthGen", "iPhone6Plus", "iPhone4", "iPad23G", "iPhone5s"],
"advisories":["Infrequent/Mild Alcohol, Tobacco, or Drug Use or References", "Infrequent/Mild Mature/Suggestive Themes", "Infrequent/Mild Profanity or Crude Humor", "Infrequent/Mild Sexual Content and Nudity"], "averageUserRatingForCurrentVersion":3.0, "artworkUrl100":"http://is5.mzstatic.com/image/pf/us/r30/Purple5/v4/85/2d/45/852d4578-89ba-8db4-52fa-b93fa95ebfc6/mzl.coqjmrmb.png", "trackCensoredName":"Yelp", "languageCodesISO2A":["NB", "ZH", "CS", "DA", "NL", "EN", "FI", "FR", "DE", "IT", "JA", "MS", "PL", "PT", "ZH", "ES", "SV", "ZH", "TR"], "fileSizeBytes":"46725787", "contentAdvisoryRating":"12+", "userRatingCountForCurrentVersion":91, "trackViewUrl":"https://itunes.apple.com/us/app/yelp/id284910350?mt=8&uo=4", "trackContentRating":"12+", "currency":"USD", "wrapperType":"software", "version":"9.10.1", "artistId":284910353, "artistName":"Yelp", "genres":["Travel", "Navigation"], "price":0.00,
"description":"Top-ranked Yelp for your iPhone, iPad, and Apple Watch has over 70 million reviews on businesses worldwide! Whether you are looking for a pizzeria that is open now or a coffee shop nearby, Yelp is your local guide to finding just the place to eat, shop, drink, relax, and play.\n\n• Discover great local businesses.\n• Search for nearby restaurants, shops, and services.\n• Filter search results by neighborhood, distance, rating, price, and what’s open now.\n• Read millions of reviews written by a community of active, expert locals.\n• Get to know a business through beautiful photos.\n• Find great Deals offered by your favorite local businesses.\n• Look up addresses and phone numbers, call a business, or make reservations directly from the app.\n• Write reviews, check-in to businesses, upload photos, and add tips of your own!", "releaseDate":"2008-07-11T07:00:00Z", "sellerName":"Yelp, Inc.", "bundleId":"com.yelp.yelpiphone", "genreIds":["6003", "6010"], "trackId":284910350, "trackName":"Yelp", "primaryGenreName":"Travel", "primaryGenreId":6003, "releaseNotes":"• Bug fixes and various improvements", "minimumOsVersion":"7.0", "formattedPrice":"Free", "averageUserRating":4.0, "userRatingCount":194085}]
}
/*:
To test out, copy the code into a Playground file and add `example.json` to the `Resources` folder of the Playground.
*/
import Foundation
infix operator >>- {associativity left}
func >>- <A,B>(x:A?, f:A -> B?) -> B? {
if let x = x { return f(x) }
return .None
}
typealias JSONDict = [String: AnyObject]
func toJSONArray(key:String)(_ j:JSONDict) -> [JSONDict]? {
return j[key] as? [JSONDict]
}
func JSONFromFile(file: String) -> JSONDict? {
return NSBundle.mainBundle().pathForResource(file, ofType: "json")
>>- { NSData(contentsOfFile: $0) }
>>- { NSJSONSerialization.JSONObjectWithData($0, options: NSJSONReadingOptions(0), error: nil) as? JSONDict}
}
struct Product {
let name: String, formattedPrice: String, averageUserRating: Float?, trackContentRating: String
}
func productFromJSON(j:JSONDict) -> Product? {
if let name = j["trackName"] as? String,
price = j["formattedPrice"] as? String,
rating = j["trackContentRating"] as? String {
return Product(
name: name,
formattedPrice: price,
averageUserRating: (j["averageUserRating"] as? Float),
trackContentRating: rating
)
}
return .None
}
let json = JSONFromFile("example")
let product = json
>>- toJSONArray("results")
>>- first
>>- productFromJSON
product?.name // Yelp
product?.formattedPrice // Free
product?.averageUserRating // 4
product?.trackContentRating // 12+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment