需要编码为具有2个Date实例变量(日期和时间)的结构JSON,但是,我需要使用不同的格式(即)对每个date实例变量进行编码。代表“ day”:“ yyyy-M-d”和“ time”:“ H:m:s”。
已经编写了一个自定义解码器,该解码器没有问题。但不确定如何编写所需的自定义编码器来解决此问题。
例如,我可以解码以下JSON字符串: {“ biometrics”:[ {“ biometricId”:1,“金额”:2.1,“来源”:“ Alderaan”,“天”:“ 2019-1-3”,“时间”:“ 11-3-3”,“ unitId”:2 }, {“ biometricId”:10,“ amount”:3.1,“ source”:“ Endoor”,“ day”:“ 2019-2-4”,“ time”:“ 11-4-4”,“ unitId”:20 }] }
但是,当我对其进行编码时,我只能将其编码为单一日期格式:( 帮助,将不胜感激。 谢谢。
import UIKit
let biometricsJson = """
{ "biometrics" : [
{"biometricId":1,"amount":2.1,"source":"Alderaan","day":"2019-1-3","time":"11-3-3","unitId":2},
{"biometricId":10,"amount":3.1,"source":"Endoor","day":"2019-2-4","time":"11-4-4","unitId":20}]
}
"""
struct Biometrics: Codable {
var biometrics: [Biometric]
}
struct Biometric: Codable {
var biometricId: Int
var unitId: Int
var source: String?
var amount: Double
var day: Date
var time: Date
init(biometricId: Int, unitId: Int, source: String, amount: Double, day: Date, time: Date){
self.biometricId = biometricId
self.unitId = unitId
self.source = source
self.amount = amount
self.day = day
self.time = time
}
}
extension Biometric {
static let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "H:m:s"
if let date = formatter.date(from: dateString) {
return date
}
formatter.dateFormat = "yyyy-M-d"
if let date = formatter.date(from: dateString) {
return date
}
throw DecodingError.dataCorruptedError(in: container,
debugDescription: "Cannot decode date string \(dateString)")
}
return decoder
}()
}
let biometrics = try Biometric.decoder.decode(Biometrics.self, from:biometricsJson.data(using: .utf8)!)
let jsonEncoder = JSONEncoder()
let encodedJson = try jsonEncoder.encode(biometrics)
let jsonString = String(data: encodedJson, encoding: .utf8)
if biometricsJson != jsonString {
print("error: decoding, then encoding does not give the same string")
print("biometricsJson: \(biometricsJson)")
print("jsonString: \(jsonString!)")
}
我希望编码的JSON可被解码器解码。 即biometricsJson == jsonString
答案 0 :(得分:1)
在自定义encode(to:)
中,只需使用所需的格式化程序将每个编码为字符串即可。 JSON中没有“日期”类型;这只是一个字符串。遵循以下原则:
enum CodingKeys: CodingKey {
case biometricId, amount, source, day, time, unitId
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(biometricId, forKey: .biometricId)
try container.encode(unitId, forKey: .unitId)
try container.encode(source, forKey: .source)
try container.encode(amount, forKey: .amount)
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "H:m:s"
let timeString = formatter.string(from: time)
try container.encode(timeString, forKey: .time)
formatter.dateFormat = "yyyy-M-d"
let dayString = formatter.string(from: day)
try container.encode(dayString, forKey: .day)
}
但是请注意,您无法测试等效的字符串。 JSON字典不保留顺序,因此无法保证逐个字符匹配。
请注意,如果您确实想有时间和时间,则应考虑使用DateComponents
而不是Date
。日期是时间中的特定实例;它不在任何时区,也不能只是一个小时,一分钟和一秒钟。
此外,您使用Double会导致舍入差异。因此2.1将被编码为2.1000000000000001。如果这是一个问题,则应将Decimal
的{{1}}而不是amount
。