我有以下代码来提取编码密钥中包含的JSON:
let value = try! decoder.decode([String:Applmusic].self, from: $0["applmusic"])
这将成功处理以下 JSON:
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry",
}
但是,无法从以下 JSON 中提取具有applmusic
编码密钥的 JSON:
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry",
},
"spotify":{
"differentcode":"SPOT",
"music_quality":"good",
"spotify_specific_code":"absent in apple"
},
"amazon":{
"amzncode":"SPOT",
"music_quality":"good",
"stanley":"absent in apple"
}
}
applmusic
、spotify
和amazon
的数据模型是不同的。但是,我只需要提取applmusic
并省略其他编码键。
我的Swift
数据模型如下:
public struct Applmusic: Codable {
public let code: String
public let quality: String
public let line: String
}
API 使用完整的 JSON 进行响应,我不能要求它只给我所需的字段。
如何仅解码 json 的特定部分?似乎,Decodable
要求我首先反序列化整个 json,所以我必须知道它的完整数据模型。
显然,解决方案之一是创建一个单独的Response
模型来包含applmusic
参数,但它看起来像一个黑客:
public struct Response: Codable {
public struct Applmusic: Codable {
public let code: String
public let quality: String
public let line: String
}
// The only parameter is `applmusic`, ignoring the other parts - works fine
public let applmusic: Applmusic
}
你能提出一种更好的方法来处理这样的JSON结构吗?
多一点见解
我在通用扩展中使用以下技术,自动为我解码 API 响应。因此,我更愿意概括一种处理此类情况的方法,而无需创建Root
结构。如果我需要的密钥是 JSON 结构中的 3 层,该怎么办?
这是为我进行解码的扩展:
extension Endpoint where Response: Swift.Decodable {
convenience init(method: Method = .get,
path: Path,
codingKey: String? = nil,
parameters: Parameters? = nil) {
self.init(method: method, path: path, parameters: parameters, codingKey: codingKey) {
if let key = codingKey {
guard let value = try decoder.decode([String:Response].self, from: $0)[key] else {
throw RestClientError.valueNotFound(codingKey: key)
}
return value
}
return try decoder.decode(Response.self, from: $0)
}
}
}
API 定义如下:
extension API {
static func getMusic() -> Endpoint<[Applmusic]> {
return Endpoint(method: .get,
path: "/api/music",
codingKey: "applmusic")
}
}
更新:我从这个答案中扩展了JSONDecoder
,你可以在这里检查: https://github.com/aunnnn/NestedDecodable,它允许您使用键路径解码任何深度的嵌套模型。
你可以像这样使用它:
let post = try decoder.decode(Post.self, from: data, keyPath: "nested.post")
您可以创建一个Decodable
包装器(例如,ModelResponse
此处),并将所有逻辑放入提取嵌套模型,其中有一个键:
struct DecodingHelper {
/// Dynamic key
private struct Key: CodingKey {
let stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}
let intValue: Int?
init?(intValue: Int) {
return nil
}
}
/// Dummy model that handles model extracting logic from a key
private struct ModelResponse<NestedModel: Decodable>: Decodable {
let nested: NestedModel
public init(from decoder: Decoder) throws {
let key = Key(stringValue: decoder.userInfo[CodingUserInfoKey(rawValue: "my_model_key")!]! as! String)!
let values = try decoder.container(keyedBy: Key.self)
nested = try values.decode(NestedModel.self, forKey: key)
}
}
static func decode<T: Decodable>(modelType: T.Type, fromKey key: String) throws -> T {
// mock data, replace with network response
let path = Bundle.main.path(forResource: "test", ofType: "json")!
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let decoder = JSONDecoder()
// ***Pass in our key through `userInfo`
decoder.userInfo[CodingUserInfoKey(rawValue: "my_model_key")!] = key
let model = try decoder.decode(ModelResponse<T>.self, from: data).nested
return model
}
}
您可以通过JSONDecoder
("my_model_key"
)userInfo
传递所需的密钥。然后将其转换为ModelResponse
内部的动态Key
,以实际提取模型。
然后你可以像这样使用它:
let appl = try DecodingHelper.decode(modelType: Applmusic.self, fromKey: "applmusic")
let amazon = try DecodingHelper.decode(modelType: Amazon.self, fromKey: "amazon")
let spotify = try DecodingHelper.decode(modelType: Spotify.self, fromKey: "spotify")
print(appl, amazon, spotify)
完整代码: https://gist.github.com/aunnnn/2d6bb20b9dfab41189a2411247d04904
奖励:深度嵌套键
玩了更多之后,我发现您可以使用此修改后的ModelResponse
轻松解码任意深度的密钥:
private struct ModelResponse<NestedModel: Decodable>: Decodable {
let nested: NestedModel
public init(from decoder: Decoder) throws {
// Split nested paths with '.'
var keyPaths = (decoder.userInfo[CodingUserInfoKey(rawValue: "my_model_key")!]! as! String).split(separator: ".")
// Get last key to extract in the end
let lastKey = String(keyPaths.popLast()!)
// Loop getting container until reach final one
var targetContainer = try decoder.container(keyedBy: Key.self)
for k in keyPaths {
let key = Key(stringValue: String(k))!
targetContainer = try targetContainer.nestedContainer(keyedBy: Key.self, forKey: key)
}
nested = try targetContainer.decode(NestedModel.self, forKey: Key(stringValue: lastKey)!)
}
然后你可以像这样使用它:
let deeplyNestedModel = try DecodingHelper.decode(modelType: Amazon.self, fromKey: "nest1.nest2.nest3")
从这个 json:
{
"apple": { ... },
"amazon": {
"amzncode": "SPOT",
"music_quality": "good",
"stanley": "absent in apple"
},
"nest1": {
"nest2": {
"amzncode": "Nest works",
"music_quality": "Great",
"stanley": "Oh yes",
"nest3": {
"amzncode": "Nest works, again!!!",
"music_quality": "Great",
"stanley": "Oh yes"
}
}
}
}
完整代码:https://gist.github.com/aunnnn/9a6b4608ae49fe1594dbcabd9e607834
你真的不需要嵌套结构Applmusic
在Response
.这将完成这项工作:
import Foundation
let json = """
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry"
},
"I don't want this":"potatoe",
}
"""
public struct Applmusic: Codable {
public let code: String
public let quality: String
public let line: String
}
public struct Response: Codable {
public let applmusic: Applmusic
}
if let data = json.data(using: .utf8) {
let value = try! JSONDecoder().decode(Response.self, from: data).applmusic
print(value) // Applmusic(code: "AAPL", quality: "good", line: "She told me don't worry")
}
编辑:处理您的最新评论
如果 JSON 响应以嵌套applmusic
标记的方式更改,则只需正确更改Response
类型。例:
新的 JSON(请注意,applmusic
现在嵌套在新的responseData
标记中):
{
"responseData":{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry"
},
"I don't want this":"potatoe",
}
}
唯一需要的更改是Response
:
public struct Response: Decodable {
public let applmusic: Applmusic
enum CodingKeys: String, CodingKey {
case responseData
}
enum ApplmusicKey: String, CodingKey {
case applmusic
}
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let applmusicKey = try values.nestedContainer(keyedBy: ApplmusicKey.self, forKey: .responseData)
applmusic = try applmusicKey.decode(Applmusic.self, forKey: .applmusic)
}
}
之前的更改不会分解任何现有代码,我们只是微调Response
如何解析 JSON 数据以正确获取Applmusic
对象的私有实现。所有调用(如JSONDecoder().decode(Response.self, from: data).applmusic
)将保持不变。
提示
最后,如果你想完全隐藏Response
包装器逻辑,你可能有一个公共/公开的方法来完成所有的工作;例如:
// (fine-tune this method to your needs)
func decodeAppleMusic(data: Data) throws -> Applmusic {
return try JSONDecoder().decode(Response.self, from: data).applmusic
}
隐藏Response
甚至存在的事实(使其成为私有/无法访问),将允许您通过应用程序获得所有代码,只需调用decodeAppleMusic(data:)
。例如:
if let data = json.data(using: .utf8) {
let value = try! decodeAppleMusic(data: data)
print(value) // Applmusic(code: "AAPL", quality: "good", line: "She told me don't worry")
}
推荐阅读:
编码和解码自定义类型
https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
有趣的问题。我知道那是 2 周前,但我想知道 如何使用我创建的库 KeyedCodable 来解决它。这是我对通用的主张:
struct Response<Type>: Codable, Keyedable where Type: Codable {
var responseObject: Type!
mutating func map(map: KeyMap) throws {
try responseObject <-> map[map.userInfo.keyPath]
}
init(from decoder: Decoder) throws {
try KeyedDecoder(with: decoder).decode(to: &self)
}
}
帮助程序扩展:
private let infoKey = CodingUserInfoKey(rawValue: "keyPath")!
extension Dictionary where Key == CodingUserInfoKey, Value == Any {
var keyPath: String {
set { self[infoKey] = newValue }
get {
guard let key = self[infoKey] as? String else { return "" }
return key
}
}
用:
let decoder = JSONDecoder()
decoder.userInfo.keyPath = "applmusic"
let response = try? decoder.decode(Response<Applmusic>.self, from: jsonData)
请注意,keyPath可能会嵌套得更深,我的意思是它可能是例如。"响应数据.服务.苹果音乐"。
此外,响应是可编码的,因此您无需任何额外工作即可对其进行编码。