Swift 按 ID 编码的地图子项
Swift Codable Map Children by ID
我有以下代码。
import Foundation
let jsonData = """
[
{"id": "1", "firstname": "Tom", "lastname": "Smith", "age": "28"},
{"id": "2", "firstname": "Bob", "lastname": "Smith"},
{"id": "3", "firstname": "Jim", "lastname": "Smith", "parentid": "2"},
{"id": "4", "firstname": "Ray", "lastname": "Smith", "parentid": "3"}
]
""".data(using: .utf8)!
class Person: Codable {
let id: String
let firstName, lastName: String
let age: String?
let parentid: String?
let children: [Person]?
enum CodingKeys : String, CodingKey {
case firstName = "firstname"
case lastName = "lastname"
case age
case parentid
case id
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
firstName = try values.decode(String.self, forKey: .firstName)
lastName = try values.decode(String.self, forKey: .lastName)
age = try values.decodeIfPresent(String.self, forKey: .age)
parentid = try values.decodeIfPresent(String.self, forKey: .parentid)
id = try values.decodeIfPresent(String.self, forKey: .id)
}
}
let decoded = try JSONDecoder().decode([Person].self, from: jsonData)
print(decoded)
所以我有 children
属性。我基本上希望 decoded
是一个包含 2 个 Person
对象(Tom 和 Bob)的数组。 decoded[1].children
应该是一个包含 1 个 Person
对象的数组 (Jim)。 decoded[1].children[0]
应该是一个包含 1 个 Person
对象 (Ray) 的数组。
如何使用 Swift Codable 系统实现此目的?
您可以这样做:
if let decoded = try? JSONDecoder().decode([Person].self, from: jsonData)
{
for person in decoded
{
person.children = decoded.filter({ (child) -> Bool in
return person.id == child.parentid
})
}
}
在上面的代码中,在获得 decoded
数组后,我只是使用 for loop
遍历它来识别每个 person
的 children
(如果存在)。
我有以下代码。
import Foundation
let jsonData = """
[
{"id": "1", "firstname": "Tom", "lastname": "Smith", "age": "28"},
{"id": "2", "firstname": "Bob", "lastname": "Smith"},
{"id": "3", "firstname": "Jim", "lastname": "Smith", "parentid": "2"},
{"id": "4", "firstname": "Ray", "lastname": "Smith", "parentid": "3"}
]
""".data(using: .utf8)!
class Person: Codable {
let id: String
let firstName, lastName: String
let age: String?
let parentid: String?
let children: [Person]?
enum CodingKeys : String, CodingKey {
case firstName = "firstname"
case lastName = "lastname"
case age
case parentid
case id
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
firstName = try values.decode(String.self, forKey: .firstName)
lastName = try values.decode(String.self, forKey: .lastName)
age = try values.decodeIfPresent(String.self, forKey: .age)
parentid = try values.decodeIfPresent(String.self, forKey: .parentid)
id = try values.decodeIfPresent(String.self, forKey: .id)
}
}
let decoded = try JSONDecoder().decode([Person].self, from: jsonData)
print(decoded)
所以我有 children
属性。我基本上希望 decoded
是一个包含 2 个 Person
对象(Tom 和 Bob)的数组。 decoded[1].children
应该是一个包含 1 个 Person
对象的数组 (Jim)。 decoded[1].children[0]
应该是一个包含 1 个 Person
对象 (Ray) 的数组。
如何使用 Swift Codable 系统实现此目的?
您可以这样做:
if let decoded = try? JSONDecoder().decode([Person].self, from: jsonData)
{
for person in decoded
{
person.children = decoded.filter({ (child) -> Bool in
return person.id == child.parentid
})
}
}
在上面的代码中,在获得 decoded
数组后,我只是使用 for loop
遍历它来识别每个 person
的 children
(如果存在)。