嵌套的Json Decoder Swift

时间:2018-01-22 05:52:39

标签: ios json swift json-deserialization

解码json时,我得到了无响应。我已经嵌套了json。我是否不知道,我正在解析正确的方式。 这是我的代码。

struct ProfileModelClass : Codable {
    let status : Int?
    let message : String?
    let profile : Profile?

    enum CodingKeys: String, CodingKey {

        case status = "status"
        case message = "message"
        case profile
    }
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        status = try values.decodeIfPresent(Int.self, forKey: .status)
        message = try values.decodeIfPresent(String.self, forKey: .message)
        profile = try Profile(from: decoder)
    }
}


struct Profile : Codable {
    let name : String?
    let email : String?
    let verified : Bool?
    let phone : Int?
    let articletype : [String]?
    let ai_score : Int?
    let bfi : String?
    let pic : String?
    let cover : String?
    let background : String?
    let layout : String?
    let customcolors : Customcolors?
    let widgets : [String]?
    let basket : [Basket]?
    let joindate : String?
    let linklink_id : String?
    let gps : Bool?
    let radius : Int?
    let showme : Bool?

    enum CodingKeys: String, CodingKey {

        case name = "name"
        case email = "email"
        case verified = "verified"
        case phone = "phone"
        case articletype = "articletype"
        case ai_score = "ai_score"
        case bfi = "bfi"
        case pic = "pic"
        case cover = "cover"
        case background = "background"
        case layout = "layout"
        case customcolors
        case widgets = "widgets"
        case basket = "basket"
        case joindate = "joindate"
        case linklink_id = "linklink_id"
        case gps = "gps"
        case radius = "radius"
        case showme = "showme"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        name = try values.decodeIfPresent(String.self, forKey: .name)
        email = try values.decodeIfPresent(String.self, forKey: .email)
        verified = try values.decodeIfPresent(Bool.self, forKey: .verified)
        phone = try values.decodeIfPresent(Int.self, forKey: .phone)
        articletype = try values.decodeIfPresent([String].self, forKey: .articletype)
        ai_score = try values.decodeIfPresent(Int.self, forKey: .ai_score)
        bfi = try values.decodeIfPresent(String.self, forKey: .bfi)
        pic = try values.decodeIfPresent(String.self, forKey: .pic)
        cover = try values.decodeIfPresent(String.self, forKey: .cover)
        background = try values.decodeIfPresent(String.self, forKey: .background)
        layout = try values.decodeIfPresent(String.self, forKey: .layout)
        customcolors = try Customcolors(from: decoder)
        widgets = try values.decodeIfPresent([String].self, forKey: .widgets)
        basket = try values.decodeIfPresent([Basket].self, forKey: .basket)
        joindate = try values.decodeIfPresent(String.self, forKey: .joindate)
        linklink_id = try values.decodeIfPresent(String.self, forKey: .linklink_id)
        gps = try values.decodeIfPresent(Bool.self, forKey: .gps)
        radius = try values.decodeIfPresent(Int.self, forKey: .radius)
        showme = try values.decodeIfPresent(Bool.self, forKey: .showme)
    }

}

struct Basket : Codable {
    let mood : String?
    let score : Int?

    enum CodingKeys: String, CodingKey {

        case mood = "mood"
        case score = "score"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        mood = try values.decodeIfPresent(String.self, forKey: .mood)
        score = try values.decodeIfPresent(Int.self, forKey: .score)
    }

}

struct Customcolors : Codable {
    let text : String?
    let opacity : Double?
    let bg : String?

    enum CodingKeys: String, CodingKey {

        case text = "text"
        case opacity = "opacity"
        case bg = "bg"
    }

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        text = try values.decodeIfPresent(String.self, forKey: .text)
        opacity = try values.decodeIfPresent(Double.self, forKey: .opacity)
        bg = try values.decodeIfPresent(String.self, forKey: .bg)
    }

}

这是我的json回复

  

{“status”:1,“message”:“获取数据”,“个人资料”:{“name”:“Prem Kumar”,“email”:“a@a.com”,“已验证”:true , “手机”: “+ 91998532542”, “articletype”: “好玩”, “文章”, “洞察”], “ai_score”:100, “BFI”: “100%”, “PIC”: “”, “覆盖”: “”, “背景”: “”, “布局”: “3column”, “customcolors”:{ “文本”: “#e7e7e7”, “不透明性”: “0.8”, “BG”:“# 272323 “},” 小部件 “:” 兴趣 “ ”教育“, ”兴趣爱好“, ”媒体“, ”习惯“, ”专业“, ”天气“, ”smabusinesstypes“, ”orderhistory“, ”报纸“,”国”, “愿望清单”], “篮”:[{ “心情”: “悲伤”, “得分”:5},{ “心情”: “恐惧”, “得分”:4},{ “心情”: “愤怒”, “分数”:4},{ “情绪”: “厌恶”, “分数”:3},{ “情绪”: “讨厌”, “分数”:3},{ “情绪”:“嫉妒”, “得分”:2},{ “心情”: “满意”, “得分”:0},{ “心情”: “竞技”, “得分”:0},{ “心情”: “沮丧”, “分数”:0},{ “情绪”: “欢乐”, “分数”:0},{ “情绪”: “海拔”, “分数”:0},{ “情绪”: “爱”,“分数“:0},{” 心情 “:” 有活力”, “得分”:0}], “joindate”: “2017-12-10T07:50:06.379Z”, “linklink_id”: “5a435b0a5c23904f78b76542”, “GPS” :真, “半径”:5 “SHOWME”:真}}

2 个答案:

答案 0 :(得分:0)

请阅读错误消息。他们很清楚

  • Profile中,密钥phone的值为String
  • CustomColors中,密钥opacity的值为String

双引号中的每个值都是String,甚至是"0""1.0""false"

您可以删除所有编码密钥和所有初始化程序,因为它们是隐式提供的。并且不要随意声明一切都是可选的。仅将那些属性声明为可选,其相应的键可能会丢失。此特定JSON在没有任何可选属性的情况下工作

答案 1 :(得分:0)

正如@vadian正确地说的那样,删除所有的初始化程序,并且不要将所有内容声明为可选的直接。确定响应中可能缺少的键,并仅将这些键声明为可选项。您提供的JSON不需要任何选项。

为了更清楚,请查看以下代码

struct ProfileModelClass : Codable {
    let status : Int
    let message : String
    let profile : Profile
}

struct Profile : Codable {
    let name : String
    let email : String
    let verified : Bool
    let phone : String
    let articletype : [String]
    let ai_score : Int
    let bfi : String
    let pic : String
    let cover : String
    let background : String
    let layout : String
    let customcolors : Customcolors
    let widgets : [String]
    let basket : [Basket]
    let joindate : String
    let linklink_id : String
    let gps : Bool
    let radius : Int
    let showme : Bool
}

struct Customcolors : Codable {
    let text : String
    let opacity : String
    let bg : String
}

struct Basket : Codable {
    let mood : String
    let score : Int
}