Error[8]: Undefined offset: 4, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

仅在运行时知道键时展平JSON

基础实体

struct BoringEntity: Decodable {    let id: String    let isActive: Bool    let age: Int    let company: String}

解决方案1:使用没有密钥的额外结构
/// Incomplete BoringEntity version to make Decodable conformance possible.private struct BoringEntityBare: Decodable {    let isActive: Bool    let age: Int    let company: String}// Depre to aux structprivate let depred = try! JSonDeprer().depre([String : BoringEntityBare].self, from: jsonData)// Map aux entities to BoringEntitylet entities = depred.map { BoringEntity(id: 
PhantomKeys
.key, isActive:
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 deprer: Deprer) throws {        let container = try deprer.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.depre(Int.self, forKey: .age) let company = try aux.depre(String.self, forKey: .company) let isActive = try aux.depre(Bool.self, forKey: .isActive) let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company) entities.append(entity)        }    }}let entities = try JSonDeprer().depre(BoringEntities.self, from: jsonData).entitiesprint(entities)
.value.isActive, age: [+++].value.age, company: [+++].value.company) }print(entities)

解决方案2:使用包装器

多亏了Code Different,我得以将自己的方法与他的[+++]想法结合起来,但是却无法解决:必须始终使用额外的实体。

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

仅在运行时知道键时展平JSON

基础实体

struct BoringEntity: Decodable {    let id: String    let isActive: Bool    let age: Int    let company: String}

解决方案1:使用没有密钥的额外结构
/// Incomplete BoringEntity version to make Decodable conformance possible.private struct BoringEntityBare: Decodable {    let isActive: Bool    let age: Int    let company: String}// Depre to aux structprivate let depred = try! JSonDeprer().depre([String : BoringEntityBare].self, from: jsonData)// Map aux entities to BoringEntitylet entities = depred.map { BoringEntity(id: 
PhantomKeys
.key, isActive:
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 deprer: Deprer) throws {        let container = try deprer.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.depre(Int.self, forKey: .age) let company = try aux.depre(String.self, forKey: .company) let isActive = try aux.depre(Bool.self, forKey: .isActive) let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company) entities.append(entity)        }    }}let entities = try JSonDeprer().depre(BoringEntities.self, from: jsonData).entitiesprint(entities)
.value.isActive, age: .value.age, company: [+++].value.company) }print(entities)

解决方案2:使用包装器

多亏了Code Different,我得以将自己的方法与他的[+++]想法结合起来,但是却无法解决:必须始终使用额外的实体。

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

仅在运行时知道键时展平JSON

基础实体

struct BoringEntity: Decodable {    let id: String    let isActive: Bool    let age: Int    let company: String}

解决方案1:使用没有密钥的额外结构
/// Incomplete BoringEntity version to make Decodable conformance possible.private struct BoringEntityBare: Decodable {    let isActive: Bool    let age: Int    let company: String}// Depre to aux structprivate let depred = try! JSonDeprer().depre([String : BoringEntityBare].self, from: jsonData)// Map aux entities to BoringEntitylet entities = depred.map { BoringEntity(id: 
PhantomKeys
.key, isActive:
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 deprer: Deprer) throws {        let container = try deprer.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.depre(Int.self, forKey: .age) let company = try aux.depre(String.self, forKey: .company) let isActive = try aux.depre(Bool.self, forKey: .isActive) let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company) entities.append(entity)        }    }}let entities = try JSonDeprer().depre(BoringEntities.self, from: jsonData).entitiesprint(entities)
.value.isActive, age: .value.age, company: .value.company) }print(entities)

解决方案2:使用包装器

多亏了Code Different,我得以将自己的方法与他的[+++]想法结合起来,但是却无法解决:必须始终使用额外的实体。

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

仅在运行时知道键时展平JSON

基础实体

struct BoringEntity: Decodable {    let id: String    let isActive: Bool    let age: Int    let company: String}

解决方案1:使用没有密钥的额外结构
/// Incomplete BoringEntity version to make Decodable conformance possible.private struct BoringEntityBare: Decodable {    let isActive: Bool    let age: Int    let company: String}// Depre to aux structprivate let depred = try! JSonDeprer().depre([String : BoringEntityBare].self, from: jsonData)// Map aux entities to BoringEntitylet entities = depred.map { BoringEntity(id: 
PhantomKeys
.key, isActive:
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 deprer: Deprer) throws {        let container = try deprer.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.depre(Int.self, forKey: .age) let company = try aux.depre(String.self, forKey: .company) let isActive = try aux.depre(Bool.self, forKey: .isActive) let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company) entities.append(entity)        }    }}let entities = try JSonDeprer().depre(BoringEntities.self, from: jsonData).entitiesprint(entities)
.value.isActive, age: .value.age, company: .value.company) }print(entities)

解决方案2:使用包装器

多亏了Code Different,我得以将自己的方法与他的想法结合起来,但是却无法解决:必须始终使用额外的实体。

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
仅在运行时知道键时展平JSON_随笔_内存溢出

仅在运行时知道键时展平JSON

仅在运行时知道键时展平JSON,第1张

仅在运行时知道键时展平JSON

基础实体

struct BoringEntity: Decodable {    let id: String    let isActive: Bool    let age: Int    let company: String}

解决方案1:使用没有密钥的额外结构
/// Incomplete BoringEntity version to make Decodable conformance possible.private struct BoringEntityBare: Decodable {    let isActive: Bool    let age: Int    let company: String}// Depre to aux structprivate let depred = try! JSonDeprer().depre([String : BoringEntityBare].self, from: jsonData)// Map aux entities to BoringEntitylet entities = depred.map { BoringEntity(id: 
PhantomKeys
.key, isActive:
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 deprer: Deprer) throws {        let container = try deprer.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.depre(Int.self, forKey: .age) let company = try aux.depre(String.self, forKey: .company) let isActive = try aux.depre(Bool.self, forKey: .isActive) let entity = BoringEntity(id: key.stringValue, isActive: isActive, age: age, company: company) entities.append(entity)        }    }}let entities = try JSonDeprer().depre(BoringEntities.self, from: jsonData).entitiesprint(entities)
.value.isActive, age: .value.age, company: .value.company) }print(entities)

解决方案2:使用包装器

多亏了Code Different,我得以将自己的方法与他的想法结合起来,但是却无法解决:必须始终使用额外的实体。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5008381.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存