- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前正在尝试学习 Swift,但还没有走得很远,如果这是一个简单的问题,请原谅我;我已经为此工作了几个小时,但一直无法弄清楚。
我有一个名为 Person
的 Codable
类。在这个类中,我有一个名为 birthdate
的 Date
属性。所以它看起来像这样:
class Person : Codable {
var birthdate: Date = Date()
var firstName: String = ""
var lastName: String = ""
enum CodingKeys : String, CodingKey {
case birthdate
case firstName = "first_name"
case lastName = "last_name"
}
}
我正在尝试解码我的 JSON:
[
{
"address": "302 N. 5th St.",
"base_64_image": null,
"birthdate": "2009-05-06T18:56:38.367",
"created": "2017-11-21T16:21:13",
"emergency_contact": "",
"emergency_contact_number": null,
"father_cell_number": null,
"father_home_number": null,
"father_name": null,
"first_name": "John",
"gender": 1,
"id": "d92fac59-66b9-49a5-9446-005babed617a",
"image_uri": null,
"is_inactive": false,
"last_name": "Smith",
"mother_cell_number": "1234567890",
"mother_home_number": "",
"mother_name": "His Mother",
"nickname": null,
"tenant_id": "9518352f-4855-4699-b0da-ecdc06470342",
"updated": "2018-01-20T02:11:45.9025023"
}
]
像这样:
// Fetch the data from the URL.
let headers: HTTPHeaders = [
"Accept": "application/json"
]
Alamofire.request(url, headers: headers).responseJSON { response in
if let data = response.data {
let decoder = JSONDecoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let people = try! decoder.decode(Array<Person>.self, from: data)
}
}
但是,我总是得到同样的错误:
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 47", intValue: Optional(47)), App.Person.CodingKeys.birthdate], debugDescription: "Date string does not match format expected by formatter.", underlyingError: nil))
(“索引 47”显然不准确,因为那是我的实时 [和私有(private)] 数据)。
如果我从 Person
类中取出 birthdate
属性,一切都会按预期进行。
几个小时以来,我一直在谷歌搜索和尝试新事物,但无论我怎样尝试,它仍然无法正常工作。这里有人可以帮我吗?
最佳答案
它看起来像你的生日之一:
"birthdate": "2009-05-06T18:56:38.367",
包含毫秒。您的日期格式字符串:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
无法处理这个。您可以更改传入 JSON 中的 birthdate
字段,或将 dateFormat
字符串更改为:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
请注意,添加 .SSS
似乎会破坏非毫秒日期的格式化程序。我建议减少服务器端的毫秒数。
原回答如下:
我刚刚在 Playground 中尝试过,它似乎按预期工作:
class Person : Codable {
var birthdate: Date = Date()
var firstName: String = ""
var lastName: String = ""
enum CodingKeys : String, CodingKey {
case birthdate
case firstName = "first_name"
case lastName = "last_name"
}
}
var json: String = """
[
{
"birthdate": "2009-05-06T18:56:38",
"first_name": "John",
"last_name": "Smith"
}
]
"""
let decoder = JSONDecoder()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
decoder.dateDecodingStrategy = .formatted(dateFormatter)
let people = try! decoder.decode(Array<Person>.self, from: json.data(using: .utf8, allowLossyConversion: false)!)
people
现在是这样的:
{birthdate "May 6, 2009 at 6:56 PM", firstName "John", lastName "Smith"}
要么我的代码与您的代码略有不同,要么可能需要一组不同的示例数据。
关于ios - 无法在 Swift 4 中解码日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49343500/
我有以下 json: {"results": [{"columns":["room_id","player_name","player_ip"], "types":["integer","text
我在 go 中获取格式不一致的 JSON 文件。例如,我可以有以下内容: {"email": "\"blah.blah@blah.com\""} {"email": "robert@gmail.com
JavaScript中有JSON编码/解码base64编码/解码函数吗? 最佳答案 是的,btoa() 和 atob() 在某些浏览器中可以工作: var enc = btoa("this is so
我在其中一个项目中使用了 Encog,但在解码 One-Of Class 时卡住了。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预
在我的 previous question关于使用 serialize() 创建对象的 CSV 我从 jmoy 那里得到了一个很好的答案,他推荐了我的序列化文本的 base64 编码。这正是我要找的。
有些事情让我感到困惑 - 为什么 this image在每个浏览器中显示不同? IE9(和 Windows 照片查看器)中的图像: Firefox(和 Photoshop)中的图像: Chrome(和
是否可以在不知道它的类型( JAXBContext.newInstance(clazz) )的情况下解码一个类,或者什么是测试即将到来的正确方法? 我确实收到了从纯文本中解码的消息 - 字符串 传入的
我正在尝试使用 openSSL 库进行 Base64 解码,然后使用 CMS 来验证签名。 下面的代码总是将缓冲区打印为 NULL。 char signed_data[] = "MIIO"; int
我有一个带有 SEL 类型实例变量的类,它是对选择器的引用。在encodeWithCoder/initWithCoder中,如何编码/解码这种类型的变量? 最佳答案 您可以使用 NSStringFro
var url = 'http://www.googleapis.com/customsearch/v1?q=foo&searchType=image'; window.fetch(url) .t
我想知道Android 2.2、2.3和3,4支持的音频/视频格式列表。我也想知道哪些Android版本支持视频编码和解码。我经历了this link,但是关于编码和解码我并不清楚。 任何人的回答都是
我在其中一个项目中使用 Encog,但在解码 One-Of 类时遇到了困难。该字段的规范化操作之一是 NormalizationAction.OneOf,它具有三个输出。当我评估时,我想解码预测值。如
我正在尝试解码现有的 xml 文件,以便我可以正确处理数据,但 XML 结构看起来很奇怪。下面是 xml 示例以及我创建的对象。 11 266 AA1001 1
对 unicode 字符进行 URL 编码的常用方法是将其拆分为 2 %HH 代码。 (\u4161 => %41%61) 但是,unicode在解码时是如何区分的呢?您如何知道 %41%61 是 \
我正在尝试将 json 字符串解码为 Map。 我知道有很多这样的问题,但我需要非常具体的格式。例如,我有 json 字符串: { "map": { "a": "b",
我有一个查询,我认为需要像这样(解码会更大) SELECT firstName, lastName, decode(mathMrk, 80, 'A', mathMrk) as decodeMat
我知道PHP函数encode()和decode(),它们对我来说工作得很好,但我想在url中传递编码字符串,但encode确实返回特殊字符,如“=”、“”' “等等...... 这显然会破坏我的脚本,
我必须解码 Basic bW9uTG9naW46bW9uTW90RGVQYXNz 形式的 http 请求的授权 header 当我解码它时online ,我得到了正确的结果 monLogin:monM
这个问题已经有答案了: Decode Base64 data in Java (21 个回答) 已关闭 8 年前。 我想知道使用哪个库进行 Base64 编码/解码?我需要此功能足够稳定以供生产使用。
我正在尝试从 Arduino BT 解码 []byte,我的连接完美,问题是当我尝试解码数组时。我得到的只是这个字符�(发送的字节数相同)我认为问题出在解码上。我尝试使用 ASCII 字符集,但仍然存
我是一名优秀的程序员,十分优秀!