Swift 4 Codable:将JSON返回字符串转换为Int/Date/Float [英] Swift 4 Codable: Converting JSON return String to Int/Date/Float
问题描述
我正在研究一些项目并删除JSON解析框架,因为使用Swift 4看起来很简单.我遇到了这个奇怪的JSON返回,其中Ints
和Dates
作为Strings
返回.
I'm going through some projects and removing JSON parsing frameworks, as it seems pretty simple to do with Swift 4. I've encountered this oddball JSON return where Ints
and Dates
are returned as Strings
.
我查看了 GrokSwift使用Swift 4解析JSON ,
I looked at GrokSwift's Parsing JSON with Swift 4, Apple's website, but I don't see anything that jumps out re: changing types.
Apple的示例代码显示了如何更改键名,但是我很难弄清楚如何更改密钥类型.
Apple's example code shows how to change key names, but I'm having a hard time figuring out how to change the key type.
这是它的样子:
{
"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"
}
]
}
我已经使用CodingKey
将字典键重命名为符合Swift的条目,如下所示:
I've used CodingKey
to rename dictionary keys to a Swift-conforming entry, 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"
}
}
}
这仍然让我留下应该是Int
或Date
的String
.我该如何使用Codable协议将包含Int/Date/Float
作为String
的JSON返回值转换为Int/Date/Float
?
That still leaves me with String
that should be Int
or Date
. How would I go about converting a JSON return that contains an Int/Date/Float
as a String
to an Int/Date/Float
using the Codable protocol?
推荐答案
This is not yet possible as Swift team has provided only String to date decoder in JSONDecoder.
您始终可以通过以下方式手动进行解码:
You can always decode manually though:
struct WaitTimeContainer: Decodable {
let waitTimes: [WaitTime]
private enum CodingKeys: String, CodingKey {
case waitTimes = "WaitTimes"
}
struct WaitTime:Decodable {
let checkpointIndex: Int
let waitTime: Float
let createdDateTime: Date
init(checkpointIndex: Int, waitTime: Float, createdDateTime:Date) {
self.checkpointIndex = checkpointIndex
self.waitTime = waitTime
self.createdDateTime = createdDateTime
}
static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"
return formatter
}()
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let checkpointIndexString = try container.decode(String.self, forKey: .checkpointIndex)
let checkpointIndex = Int(checkpointIndexString)!
let waitTimeString = try container.decode(String.self, forKey: .waitTime)
let waitTime = Float(waitTimeString)!
let createdDateTimeString = try container.decode(String.self, forKey: .createdDateTime)
let createdDateTime = WaitTime.formatter.date(from: createdDateTimeString)!
self.init(checkpointIndex:checkpointIndex, waitTime:waitTime, createdDateTime:createdDateTime)
}
private enum CodingKeys: String, CodingKey {
case checkpointIndex = "CheckpointIndex"
case waitTime = "WaitTime"
case createdDateTime = "Created_Datetime"
}
}
}
这篇关于Swift 4 Codable:将JSON返回字符串转换为Int/Date/Float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!