JSON日期解码Swift

时间:2018-07-11 18:49:30

标签: ios json swift jsondecoder

在JSON中解码日期时出错:

我的JSON:

{
    patientDetails =     {
        "_id" = 5b432753052aaca64879335c;
        birthDate = "1000-08-02T00:00:00.000Z";
        firstName = Mary;
        lastName = Smith;
    };
}

班级:

class Patient: Person, Codable {
    var birthDate: Date
    ....
}

编码:

let jsonData = try JSONSerialization.data(withJSONObject: json, options: [])
                    let decoder = JSONDecoder()
                    decoder.dateDecodingStrategy = .iso8601
                    let patientDic = try! decoder.decode([String:Patient].self, from: jsonData) //Runtime error
  

线程1:致命错误:“尝试!”表达式意外引发错误:   Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath:   [_DictionaryCodingKey(stringValue:“ patientDetails”,intValue:nil),   CodingKeys(stringValue:“ creationDate”,intValue:nil)],   debugDescription:“预期日期字符串为ISO8601格式。”,   底层错误:nil))

0 个答案:

没有答案