繁体   English   中英

Swift - 如何将平面 json 解码为嵌套结构?

[英]Swift - How to decode flat json to nested structure?

假设我有以下 JSON ,我想将其解码为这个特定的结构。 我该怎么做呢

JSON:

{
 "fullName": "Federico Zanetello",
 "id": 123456,
 "twitter": "http://twitter.com/zntfdr",
 "results": ["mate","bate"]
}

解码结构

struct Response {
    let data: UserData,
    let results: [String]
}

UserData结构

Struct UserData {
   let fullName: String,
   let id: Int,
   let twitter: String
}

我做了我的研究,找不到有效的解决方案。 这是我到目前为止写的代码

struct UserData: Decodable {
    let fullName: String
    let id: Int
    let twitter: String
    
    enum CodingKeys: String, CodingKey {
        case fullName
        case id
        case twitter
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.fullName = try container.decode(String.self, forKey: .fullName)
        self.id = try container.decode(Int.self, forKey: .id)
        self.twitter = try container.decode(String.self, forKey: .twitter)
    }
}

struct Respone: Decodable{
    let data: UserData
    let results: [String]
}

let json = """
{
 "fullName": "Federico Zanetello",
 "id": 123456,
 "twitter": "http://twitter.com/zntfdr",
 "results": ["mate","bate"]
}
""".data(using: .utf8)! // our data in native (JSON) format
let myStruct = try JSONDecoder().decode(MetaData.self, from: json) // Decoding our data
print(myStruct)

我收到 KeynotFound 错误,因为 JSON 中不存在data密钥。 我该如何解决这个问题?

你可以这样做:

struct UserData: Decodable {
    let fullName: String
    let id: Int
    let twitter: String
}

struct Response: Decodable{
    let data: UserData
    let results: [String]
    
    enum CodingKeys : CodingKey {
        case results
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        results = try container.decode([String].self, forKey: .results)
        data = try decoder.singleValueContainer().decode(UserData.self)
    }
}

诀窍是在Response中使用自定义解码,然后像往常一样解码results后,得到一个singleValueContainer 此容器将包含 JSON 中的所有密钥,因此您可以使用此容器来解码UserData

你只需要这个结构。数据在你的响应中不作为键出现

// MARK: - User
struct User: Codable {
    let fullName: String
    let id: Int
    let twitter: String
    let results: [String]
}

广告像这样解码

let myStruct = try JSONDecoder().decode(User.self, from: json) 

注意:如果您没有执行任何额外的操作,则不需要init() function 进行编码。如果结构元素的名称相同,则不需要CodingKeys

以下是如何将UserData用作具有interface segregation的单独 object

struct Response: UserData, Codable {
    let fullName: String
    let id: Int
    let twitter: String
    let results: [String]
}

protocol UserData {
    var fullName: String {get}
    var id: Int {get}
    var twitter: String{get}
}

  let myStruct = try JSONDecoder().decode(Response.self, from: json)

  let userData : UserData = myStruct 

  print(userData.fullName)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM