Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DhavalDobariya86/99ef4a99cdb5a6429cf316d8b5bc5382 to your computer and use it in GitHub Desktop.
Save DhavalDobariya86/99ef4a99cdb5a6429cf316d8b5bc5382 to your computer and use it in GitHub Desktop.
Codable with Key decoding strategy
import UIKit
struct Country: Codable {
let name: String
let capital: String
let callingCodes: [String]
}
let jsonData: Data = """
{
"name": "United States of America",
"capital": "Washington, D.C.",
"calling_codes": ["1"]
}
""".data(using: .utf8)!
let decoder = JSONDecoder()
// To convert from `snake-case` JSON key to `camelCase` parameter name
decoder.keyDecodingStrategy = .convertFromSnakeCase
let country = try! decoder.decode(Country.self, from: jsonData)
print("Country calling codes = \(country.callingCodes.description)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment