[英]Nested Json Decoder Swift
在解碼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響應
{“狀態”:1,“消息”:“獲取的數據”,“配置文件”:{“名稱”:“ Prem Kumar”,“電子郵件”:“ a@a.com”,“已驗證”:true,“電話“:” + 91998532542“,” articletype“:[” fun“,” article“,” insight“],” ai_score“:100,” bfi“:” 100%“,” pic“:”“,” cover“ :“”,“ background”:“”,“ layout”:“ 3column”,“ customcolors”:{“ text”:“#e7e7e7”,“ opacity”:“ 0.8”,“ bg”:“#272323”} ,“小配件”:[“興趣”,“教育”,“興趣”,“媒體”,“習慣”,“職業”,“天氣”,“社交類型”,“訂單歷史”,“報紙”,“國家”, “ wishlist”],“ basket”:[{“ mood”:“ sadness”,“ score”:5},{“ mood”:“ fear”,“ score”:4},{“ mood”:“ anger” ,“得分”:4},{“心情”:“厭惡”,“得分”:3},{“心情”:“討厭”,“得分”:3},{“心情”:“嫉妒”,“ score“:2},{” mood“:” satisfaction“,” score“:0},{” mood“:” competetive“,” score“:0},{” mood“:” frustration“,” score“ :0},{“ mood”:“歡樂”,“得分”:0},{“ mood”:“海拔”,“得分”:0},{“ mood”:“愛”,“得分”:0 },{“ mood”:“ energytic”,“ score”:0}],“ joindate”:“ 2017-12-10T07:50:06.379Z”,“ linklink_id”:“ 5a435b0a5c23904f78b76542”,“ gps”:true, “ radius”:5,“ showme”:true}}
請閱讀錯誤消息。 他們很清楚
Profile
中,按鍵phone
值為String
CustomColors
中,鍵opacity
值為String
雙引號中的每個值都是String
,甚至是"0"
, "1.0"
和"false"
您可以刪除所有編碼鍵和所有初始化器,因為它們是隱式提供的。 並且不要將所有內容都隨意聲明為可選。 僅將那些屬性聲明為可選,否則其相應的鍵可能會丟失。 此特定的JSON無需任何可選屬性即可工作
就像@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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.