Forked from cristainlika/gist:7133295e71c24bf3161bd4f8b58f3efc
Last active
March 30, 2017 14:18
-
-
Save leodabus/9531a317cdf2e26aedf2d724e7b6623b to your computer and use it in GitHub Desktop.
object model for Json parse
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
struct Contact: CustomStringConvertible { | |
let user: User | |
let address: Address | |
let deliveryInstruction: String | |
let deliveryMethod: String | |
// customize the description to your needs | |
var description: String { return "\(user.name) \(deliveryInstruction) \(deliveryMethod)" } | |
init(dictionary: [String: Any]) { | |
self.deliveryInstruction = dictionary["delivery_instruction"] as? String ?? "" | |
self.deliveryMethod = dictionary["delivery_method"] as? String ?? "" | |
self.address = Address(dictionary: dictionary["address"] as? [String: Any] ?? [:]) | |
self.user = User(dictionary: dictionary["user"] as? [String: Any] ?? [:]) | |
} | |
init?(data: Data) { | |
guard let json = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else { return nil } | |
self.init(dictionary: json) | |
} | |
init?(json: String) { | |
self.init(data: Data(json.utf8)) | |
} | |
} | |
struct User: CustomStringConvertible { | |
let name: String | |
let email: String | |
let phone: String | |
let description: String | |
init(dictionary: [String: Any]) { | |
self.name = dictionary["name"] as? String ?? "" | |
self.email = dictionary["email"] as? String ?? "" | |
self.phone = dictionary["phoneNo"] as? String ?? "" | |
self.description = "name: \(name) - email: \(email) - phone: \(phone)" | |
} | |
} | |
struct Address: CustomStringConvertible { | |
let id: String | |
let street: String | |
let house: String | |
let city: City | |
let town: Town | |
let description: String | |
init(dictionary: [String: Any] ) { | |
self.id = dictionary["address_id"] as? String ?? "" | |
self.description = dictionary["full_address"] as? String ?? "" | |
self.house = dictionary["house"] as? String ?? "" | |
self.street = dictionary["street"] as? String ?? "" | |
self.city = City(dictionary: dictionary["city"] as? [String: Any] ?? [:]) | |
self.town = Town(dictionary: dictionary["town"] as? [String: Any] ?? [:]) | |
} | |
} | |
struct City: CustomStringConvertible { | |
let id: String | |
let name: String | |
// customize the description to your needs | |
var description: String { return name } | |
init(dictionary: [String: Any] ) { | |
self.id = dictionary["city_id"] as? String ?? "" | |
self.name = dictionary["city_name"] as? String ?? "" | |
} | |
} | |
struct Town: CustomStringConvertible { | |
let id: String | |
let name: String | |
// customize the description to your needs | |
var description: String { return name } | |
init(dictionary: [String: Any]) { | |
self.id = dictionary["town_id"] as? String ?? "" | |
self.name = dictionary["town_name"] as? String ?? "" | |
} | |
} |
it is for json parse right ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing: