Created
February 6, 2018 15:40
-
-
Save Otbivnoe/0dcd1f5640f4391d12474e0b372a4aef 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
import Foundation | |
let json = """ | |
{ | |
"businesses": [ | |
{ | |
"id": "joes-pizza-new-york-4", | |
"name": "Joe's Pizza", | |
"image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/FhbFkrh_3TrAOZvoWTyOJA/o.jpg", | |
"is_closed": false, | |
"url": "https://www.yelp.com/biz/joes-pizza-new-york-4?adjust_creative=om5ZiponkpRqUNa3pVPiRg&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=om5ZiponkpRqUNa3pVPiRg", | |
"review_count": 1822, | |
"categories": [{ | |
"alias": "pizza", | |
"title": "Pizza" | |
}], | |
"rating": 4, | |
"coordinates": { | |
"latitude": 40.7305599, | |
"longitude": -74.0021299 | |
}, | |
"transactions": [], | |
"price": "$", | |
"location": { | |
"address1": "7 Carmine St", | |
"address2": "", | |
"address3": "", | |
"city": "New York", | |
"zip_code": "10014", | |
"country": "US", | |
"state": "NY", | |
"display_address": [ | |
"7 Carmine St", | |
"New York, NY 10014" | |
] | |
}, | |
"phone": "+12123661182", | |
"display_phone": "(212) 366-1182", | |
"distance": 2873.519378444 | |
} | |
] | |
} | |
""" | |
class Category: Codable { | |
let alias: String | |
let title: String | |
} | |
class Location: Codable { | |
let address1: String? | |
let address2: String? | |
let address3: String? | |
let city: String | |
let zipCode: String | |
let country: String | |
let state: String | |
let displayAddress: [String] | |
enum CodingKeys: String, CodingKey { | |
case address1 | |
case address2 | |
case address3 | |
case city | |
case zipCode = "zip_code" | |
case country | |
case state | |
case displayAddress = "display_address" | |
} | |
} | |
class Coordinate: Codable { | |
let latitude: Double | |
let longitude: Double | |
} | |
struct Business: Codable { | |
var id: String | |
let name: String | |
let imageURL: String | |
let isClosed: Bool | |
let url: String | |
let reviewCount: Int | |
let categories: [Category] | |
let rating: Int | |
let price: String? | |
let location: Location? | |
let phone: String | |
let displayPhone: String | |
let distance: Double | |
let coordinates: Coordinate | |
enum CodingKeys: String, CodingKey { | |
case id = "id" | |
case name | |
case imageURL = "image_url" | |
case isClosed = "is_closed" | |
case url | |
case reviewCount = "review_count" | |
case categories | |
case rating | |
case price | |
case location | |
case phone | |
case displayPhone = "display_phone" | |
case distance | |
case coordinates | |
} | |
} | |
class Results: Codable { | |
let businesses: [Business] | |
} | |
let data = json.data(using: .utf8)! | |
do { | |
let results = try JSONDecoder().decode(Results.self, from: data) | |
print(results.businesses) | |
} | |
catch { | |
print(error) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment