I am looking at some projects and removing the JSON framework for parsing, as it seems pretty simple for Swift 4. I came across this JSON return with odd code, where Ints
they Dates
return as Strings
.
I looked at GrokSwift Parsing JSON with Swift 4 , Apple's website , but I don't see anything that jumps out: type change.
The Apple code example shows how to change key names, but it's hard for me to determine how to change the key type.
Here's what it looks like:
{
"WaitTimes": [
{
"CheckpointIndex": "1",
"WaitTime": "1",
"Created_Datetime": "10/17/2017 6:57:29 PM"
},
{
"CheckpointIndex": "2",
"WaitTime": "6",
"Created_Datetime": "10/12/2017 12:28:47 PM"
},
{
"CheckpointIndex": "0",
"WaitTime": "8",
"Created_Datetime": "9/26/2017 5:04:42 AM"
}
]
}
I used CodingKey
to rename the dictionary keys into an entry corresponding to Swift, as follows:
struct WaitTimeContainer: Codable {
let waitTimes: [WaitTime]
private enum CodingKeys: String, CodingKey {
case waitTimes = "WaitTimes"
}
struct WaitTime: Codable {
let checkpointIndex: String
let waitTime: String
let createdDateTime: String
private enum CodingKeys: String, CodingKey {
case checkpointIndex = "CheckpointIndex"
case waitTime = "WaitTime"
case createdDateTime = "Created_Datetime"
}
}
}
String
, Int
Date
. JSON, Int/Date/Float
String
Int/Date/Float
, Codable?