我们假设我们有一个类似于以下的JSON结构(常用于Firebase的实时数据库):
{
"18348b9b-9a49-4e04-ac35-37e38a8db1e2": {
"isActive": false,
"age": 29,
"company": "BALOOBA"
},
"20aca96e-663a-493c-8e9b-cb7b8272f817": {
"isActive": false,
"age": 39,
"company": "QUONATA"
},
"bd0c389b-2736-481a-9cf0-170600d36b6d": {
"isActive": false,
"age": 35,
"company": "EARTHMARK"
}
}
使用Decodable
我希望将其转换为包含3个元素的数组:
struct BoringEntity: Decodable {
let id: String
let isActive: Bool
let age: Int
let company: String
init(from decoder: Decoder) throws {
// ...
}
}
let entities: [BoringEntity] = try! JSONDecoder()...
id 属性对应于json对象的根字符串,例如:18348b9b-9a49-4e04-ac35-37e38a8db1e2
。
我已经尝试了几种方法,但无法获取id属性而无需辅助实体(或使用选项):
/// Incomplete BoringEntity version to make Decodable conformance possible.
struct BoringEntityIncomplete: Decodable {
let isActive: Bool
let age: Int
let company: String
}
// Decode to aux struct
let decoded = try! JSONDecoder().decode([String : BoringEntityIncomplete].self, for: jsonData)
// Map aux entities to BoringEntity
let entities = decoded.map { BoringEntity(...) }
使用init(from: Decoder)
并不像其他情况那样简单,因为由于密钥未知,因此无法使用keyedContainer(,)
。
Decodable
是否不适合这些类型的案件?
答案 0 :(得分:2)
在回答你的问题之前,有几件事情:
1:评论(gesture.cancelsTouchesInView = false
)使JSON无效。 JSON不允许发表评论。
2:// id
中的id
属性来自哪里?
BoringEntity
如果我忽略了这些事情,你可以将struct BoringEntity: Decodable {
let id: String // where is it stored in the JSON???
let isActive: Bool
let age: Int
let company: String
}
的数组包装在一个结构(BoringEntity
)中。不建议直接使用BoringEntities
,因为您必须掩盖[BoringEntity]
的默认init(from decoder:)
。
这里的诀窍是让Array
通过container.allKeys
属性为您提供密钥列表:
JSONDecoder
用法:
struct BoringEntity: Decodable {
let isActive: Bool
let age: Int
let company: String
}
struct BoringEntities: Decodable {
var entities = [BoringEntity]()
// This really is just a stand-in to make the compiler happy.
// It doesn't actually do anything.
private struct PhantomKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PhantomKeys.self)
for key in container.allKeys {
let entity = try container.decode(BoringEntity.self, forKey: key)
entities.append(entity)
}
}
}
答案 1 :(得分:0)
基础实体:
struct BoringEntity: Decodable {
let id: String
let isActive: Bool
let age: Int
let company: String
}
/// Incomplete BoringEntity version to make Decodable conformance possible.
private struct BoringEntityBare: Decodable {
let isActive: Bool
let age: Int
let company: String
}
// Decode to aux struct
private let decoded = try! JSONDecoder().decode([String : BoringEntityBare].self, from: jsonData)
// Map aux entities to BoringEntity
let entities = decoded.map { BoringEntity(id: $0.key, isActive: $0.value.isActive, age: $0.value.age, company: $0.value.company) }
print(entities)
感谢Code Different我能够将我的方法与他的PhantomKeys
想法结合起来,但是没有办法解决它:必须始终使用额外的实体。
struct BoringEntities: Decodable {
var entities = [BoringEntity]()
// This really is just a stand-in to make the compiler happy.
// It doesn't actually do anything.
private struct PhantomKeys: CodingKey {
var intValue: Int?
var stringValue: String
init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
init?(stringValue: String) { self.stringValue = stringValue }
}
private enum BareKeys: String, CodingKey {
case isActive, age, company
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PhantomKeys.self)
// There's only one key
for key in container.allKeys {
let aux = try container.nestedContainer(keyedBy: BareKeys.self, forKey: key)
let age = try aux.decode(Int.self, forKey: .age)
let company = try aux.decode(String.self, forKey: .company)
let isActive = try aux.decode(Bool.self, forKey: .isActive)
let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company)
entities.append(entity)
}
}
}
let entities = try JSONDecoder().decode(BoringEntities.self, from: jsonData).entities
print(entities)