json - 从数组初始化的可解码
问题描述
我有一个 JSON 对象数组。简而言之,它具有以下形式:
[
{"name": "Tinky Winky"},
{"name": "Dipsy"},
{"name": "Laa-Laa"},
{"name": "Po"}
]
我可以创建一个结构Tubby
可以解码数组中的单个实例:
struct Tubby: Codable {
let name: String
}
我想创建一个Tubbies
可以从 JSON 数组解码的结构:
struct Tubbies: Codable {
let tubbies: [Tubby]
init(from decoder: Decoder) throws {
// What goes here?
tubbies = ???
}
......但现在我不知道应该如何解码?我只想做:
init(from decoder: Decoder) throws {
// What goes here?
tubbies = decoder.decode([Tubby].self)
}
但Decoder
不提供decode
. 它有:
/// Returns the data stored in this decoder as represented in a container
/// keyed by the given key type.
///
/// - parameter type: The key type to use for the container.
/// - returns: A keyed decoding container view into this decoder.
/// - throws: `DecodingError.typeMismatch` if the encountered stored value is
/// not a keyed container.
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey
/// Returns the data stored in this decoder as represented in a container
/// appropriate for holding values with no keys.
///
/// - returns: An unkeyed container view into this decoder.
/// - throws: `DecodingError.typeMismatch` if the encountered stored value is
/// not an unkeyed container.
func unkeyedContainer() throws -> UnkeyedDecodingContainer
/// Returns the data stored in this decoder as represented in a container
/// appropriate for holding a single primitive value.
///
/// - returns: A single value container view into this decoder.
/// - throws: `DecodingError.typeMismatch` if the encountered stored value is
/// not a single value container.
func singleValueContainer() throws -> SingleValueDecodingContainer
两个都(这是一个错误,因为答案澄清了 - 谢谢!)并singleValueContainer
unkeyedContainer
抛出数组并显示一条消息,表明它们不支持数组。我可以使用container(keyedBy:)
,我应该传递什么作为密钥?
解决方案
我认为这不是一个好方法,因为[Tubby].self
它足够简单,可以在任何地方使用。但如果你只想将数组包装在另一种类型中,你应该有这样的东西:
详细版本:
struct Tubbies: Codable {
let tubbies: [Tubby]
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
self.tubbies = try container.decode([Tubby].self)
}
}
较短的版本:
struct Tubbies: Codable {
let tubbies: [Tubby]
init(from decoder: Decoder) throws {
tubbies = try [Tubby](from: decoder)
}
}
用法:
let tubbies: Tubbies = try! JSONDecoder().decode(Tubbies.self, from: jsonData)
推荐阅读
- sql - 如果列的计数值大于 1,我想打印列的计数,否则我想在字段中打印值
- regex - 如果没有任何内容或空格分隔,则在正则表达式中连接多个字符串
- lldb - LLDB 从脚本生成命令
- java - 使用线程池创建一个等待来自客户端的消息的 java 服务器套接字
- android - Android共享画布路径绘制顺序
- c# - System.Net.Sockets.SocketException (0x80004005):没有这样的主机是已知的
- git - 使用快速导出将 Mercurial (hg) 迁移到 gitlab
- arduino - Arduino 可以同时处理中断和 Timer1 功能吗?
- mysql - MySql & Node.js - 更新多行和多列
- android - 即使在后台,如何使用蓝牙运行服务?