首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套Json解码器Swift

嵌套Json解码器Swift
EN

Stack Overflow用户
提问于 2018-01-22 05:52:39
回答 2查看 183关注 0票数 1

在解码json的时候,我没有得到任何回应。我有嵌套的json。不管我是否知道,我分析的方法是否正确。这是我的密码。

代码语言:javascript
复制
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回复

{“状态”:1、“消息”:“获取数据”、“配置文件”:{“名称”:“Prem”、“电子邮件”:“a@a.com”、“验证”:true、"phone":"+91998532542“、”文章类型“:”乐趣“、”文章“、”洞察“、"ai_score":100、bfi:”100%“、”图片“:”“、”封面“:”“、”背景“:”、“布局”:“3列”,“定制颜色”:{“文本”:“#e7e7e7”、“不透明度”:“0.8”、"bg":"#272323"}、“小部件”:“兴趣”、“教育”、“爱好”、“媒体”、“习惯”、“职业”、“天气”、"smabusinesstypes“、"orderhistory”、“报纸”、“国家”、“愿望列表”、“篮子”、{“心情”:“悲伤”、“得分”:5},{“心情”:“恐惧”,“分数”:4},{“情绪”:“愤怒”,“得分”:4},{“情绪”:“厌恶”,“分数”:3},{“情绪”:“恨”,“分数”:3},{“嫉妒”,“分数”:2},{“情绪”:“满意”,“分数”:0},{“情绪”:“竞争”,“分数”:0},{“心情”:“挫折”,“得分”:0},{“情绪”:“joy”,“得分”:0},{“情绪”:“海拔”,“得分”:0},{“情绪”:“爱”,“分数”:0},{“情绪”:“精力”,“分数”:0},“联合日期”:“2017-12-10T07:50:06.379Z”,"linklink_id":"5a435b0a5c23904f78b76542",“gps”:“真,"radius":5,“showme”:true}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-22 06:12:29

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

  • Profile中,键phone的值是String
  • CustomColors中,键opacity的值是String

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

您可以删除所有编码键和所有初始化器,因为它们是隐式提供的。不要随随便便地宣布任何事情都是可选的。只声明那些属性为可选属性,其相应的键可能丢失。这个特定的JSON工作时没有任何可选属性

票数 0
EN

Stack Overflow用户

发布于 2018-01-22 07:15:22

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

为了更清楚起见,请检查以下代码

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48375405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档