Swift 4ではかなり簡単なので、いくつかのプロジェクトを行ってJSON構文解析フレームワークを削除しています。 Ints
およびDates
はStrings
として返されます。Swift 4 Codable:JSONの文字列をInt/Date/Floatに変換する
私はGrokSwift's Parsing JSON with Swift 4,Apple's websiteを見ましたが、タイプを変更することはありません。
Apple's example codeはキー名を変更する方法を示していますが、キーの種類を変更する方法がわかりません。
ここのように見えるものです:次のように私は、スウィフト準拠のエントリに辞書のキーの名前を変更するCodingKey
を使用しました
{
"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"
}
]
}
:
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
である必要があります。 Int/Date/Float
をString
として含むJSON戻り値をInt/Date/Float
に変換するにはどうすればよいですか?
、これはまだ不可能です。https://stackoverflow.com/questions/44594652/swift- 4-json-decodable-simple-way-to-decode-type-change –
@Adrian Created_Datetimeはサーバーに格納されるときはUTC時刻で、ローカル時刻ではないことを確認します。そうでない場合は、日付フォーマッタのタイムゾーンを0秒FromGMTに設定しないでください。分あなたの日付。 –