首页 > 解决方案 > Swift 根据响应类型为 JSON 字段值动态选择 Codable 结构

问题描述

我正在使用可编码的结构来处理 API JSON 响应。其中一个字段 (slideData_) 中的数据结构取决于响应所涉及的卡片类型 (cardType)。

因此,我需要在结构中创建一个简单的条件,根据 cardType 字段中返回的值将 slideData 的值设置为 ApiCompanyProfileCardData 或 ApiPersonalProfileCardData。我一直在寻找很长一段时间,我相信 enum 和 CodingKeys 可能是要走的路,但我一直在努力实现这一点。感觉好像它应该相当简单。有人可以帮忙吗?

{"card": 
    [
    {"cardTitle": "Title 1", 
    "cardType": "companyprofile", 
    "cardTypeDesc": "Company profile", 
    "id": 10, 
    "imageFile": "b443709ca8d8a269ca185381bfa2ad8326af33c3.png", 
    "slideData": {"cp_name": "Full Name", 
                  "cp_name_font_style": "'Nunito', sans-serif", 
                  "cp_name_font_weight": "900", 
                  "cp_name_size_quantity": "30px", 
                  "cp_name_text_color": "ff0000", 
                  "cp_title": "CEO", 
                  "cp_title_font_style": "Arial", 
                  "cp_title_font_weight": "normal", 
                  "cp_title_size_quantity": "20px", 
                  "cp_title_text_color": "000000"}
                  }
     ]
}

struct ApiCardData: Codable {

    let card: [Card]

    struct Card : Codable {
        let cardTitle: String
        let cardType: String
        let id: Int
        let imageFile: String
        let slideData: ApiCompanyProfileCardData
    }

    struct ApiCompanyProfileCardData: Codable {
        let cpName: String
        let cpNameFontStyle: String
        let cpNameFontWeight: String
        let cpNameSizeQuantity: String
        let cpNameTextColor: String
        let cpTitle: String
        let cpTitleFontStyle: String
        let cpTitleFontWeight: String
        let cpTitleSizeQuantity: String
        let cpTitleTextColor: String
    }

    struct ApiPersonalProfileCardData: Codable {
        let ppEmail: String
        let ppEmailFontStyle: String
        let ppEmailFontWeight: String
        let ppEmailSizeQuantity: String
        let ppEmailTextColor: String
        let ppName: String
        let ppNameFontStyle: String
        let ppNameFontWeight: String
        let ppNameSizeQuantity: String
        let ppNameTextColor: String
    }

}

标签: swiftcodable

解决方案


@N.Widds 然后你可以像下面这样形成你的结构,希望它对你有帮助。

    struct ApiCardData: Codable {

    let card: [Card]

    struct Card : Codable {
        let cardTitle: String
        let cardType: String
        let id: Int
        let imageFile: String
        let slideData: ApiCompanyProfileCardData?
        let slideApiPersonalProfileData: ApiPersonalProfileCardData?

    }

struct ApiCompanyProfileCardData: Codable {
    let cpName: String
    let cpNameFontStyle: String

    enum CodingKeys: String, CodingKey {

        case cpName = "cp_name"
        case cpNameFontStyle = "cp_name_font_style"
    }

}

struct ApiPersonalProfileCardData: Codable {
    let ppEmail: String
    let ppEmailFontStyle: String

    enum CodingKeys: String, CodingKey {

        case ppEmail = "pp_Email"
        case ppEmailFontStyle = "pp_email_font_style"
    }

}
}

推荐阅读