Swift Playground 中没有输出的 JSONDecoder
JSONDecoder with no output in Swift Playground
我是Swift
JSON
的新手,所以我在Swift Playgrounds
练习。我很确定这将被视为解码嵌套的 JSON
密钥。但正如我所说,我是新手,并不熟悉所有的技术术语。
无论如何,我认为这段代码是正确的,但由于某种原因它无法打印。而且它没有向我显示任何错误,这使得它更难修复。但是,我一定做错了什么。
import UIKit
let jsonData :Data = """
{
"id": 1,
"name": "John Smith",
"username": "Johnny",
"email": "Johnny@yahoo.com",
"address": {
"street": "Some Street",
"suite": "100",
"city": "SomeCity",
"zipcode": "12345",
}
}
""".data(using: .utf8)!
struct User :Decodable {
let id :Int
let name :String
let userName :String
let email :String
let street :String
let suite :String
let city :String
let zipCode :String
private enum UserKeys :String, CodingKey {
case id
case name
case userName
case email
case address
}
private enum AddressKeys :String, CodingKey {
case street
case suite
case city
case zipCode
}
init(from decoder :Decoder) throws {
let userContainer = try decoder.container(keyedBy: UserKeys.self)
self.id = try userContainer.decode(Int.self, forKey: .id)
self.name = try userContainer.decode(String.self, forKey: .name)
self.userName = try userContainer.decode(String.self, forKey: .userName)
self.email = try userContainer.decode(String.self, forKey: .email)
let addressContainer = try userContainer.nestedContainer(keyedBy: AddressKeys.self, forKey: .address)
self.street = try addressContainer.decode(String.self, forKey: .street)
self.suite = try addressContainer.decode(String.self, forKey: .suite)
self.city = try addressContainer.decode(String.self, forKey: .city)
self.zipCode = try addressContainer.decode(String.self, forKey: .zipCode)
}
}
if let user = try? JSONDecoder().decode(User.self, from: jsonData) {
print(user.name)
print(user.city)
}
userName
和 zipCode
属性是驼峰式的,因为它们应该遵循 Swift
标准。但是,与 JSON
数据经常发生的情况一样,键 "username"
和 "zipcode"
都以小写形式显示。幸运的是,有一个简单的解决方法。在您的 private enums
中,只需将两个 properties
设置为小写 stringValue
,如下所示:
case userName = "username"
case zipCode = "zipcode"
从技术上讲,我认为您处理的是 nested dictionary
的 JSON properties
。不过我不确定,也许本网站上的一些专家可以详细说明这一点。
我是Swift
JSON
的新手,所以我在Swift Playgrounds
练习。我很确定这将被视为解码嵌套的 JSON
密钥。但正如我所说,我是新手,并不熟悉所有的技术术语。
无论如何,我认为这段代码是正确的,但由于某种原因它无法打印。而且它没有向我显示任何错误,这使得它更难修复。但是,我一定做错了什么。
import UIKit
let jsonData :Data = """
{
"id": 1,
"name": "John Smith",
"username": "Johnny",
"email": "Johnny@yahoo.com",
"address": {
"street": "Some Street",
"suite": "100",
"city": "SomeCity",
"zipcode": "12345",
}
}
""".data(using: .utf8)!
struct User :Decodable {
let id :Int
let name :String
let userName :String
let email :String
let street :String
let suite :String
let city :String
let zipCode :String
private enum UserKeys :String, CodingKey {
case id
case name
case userName
case email
case address
}
private enum AddressKeys :String, CodingKey {
case street
case suite
case city
case zipCode
}
init(from decoder :Decoder) throws {
let userContainer = try decoder.container(keyedBy: UserKeys.self)
self.id = try userContainer.decode(Int.self, forKey: .id)
self.name = try userContainer.decode(String.self, forKey: .name)
self.userName = try userContainer.decode(String.self, forKey: .userName)
self.email = try userContainer.decode(String.self, forKey: .email)
let addressContainer = try userContainer.nestedContainer(keyedBy: AddressKeys.self, forKey: .address)
self.street = try addressContainer.decode(String.self, forKey: .street)
self.suite = try addressContainer.decode(String.self, forKey: .suite)
self.city = try addressContainer.decode(String.self, forKey: .city)
self.zipCode = try addressContainer.decode(String.self, forKey: .zipCode)
}
}
if let user = try? JSONDecoder().decode(User.self, from: jsonData) {
print(user.name)
print(user.city)
}
userName
和 zipCode
属性是驼峰式的,因为它们应该遵循 Swift
标准。但是,与 JSON
数据经常发生的情况一样,键 "username"
和 "zipcode"
都以小写形式显示。幸运的是,有一个简单的解决方法。在您的 private enums
中,只需将两个 properties
设置为小写 stringValue
,如下所示:
case userName = "username"
case zipCode = "zipcode"
从技术上讲,我认为您处理的是 nested dictionary
的 JSON properties
。不过我不确定,也许本网站上的一些专家可以详细说明这一点。