简体   繁体   English

迅捷4-将json解析为struct(可编码),但带有一个不是json的额外变量

[英]swift 4 - Parsing json to struct (codable), but with an extra variable which is not from json

I am currently getting all the data I want from my json, but I want to add extra variable which is not from the json. 我目前正在从json获取我想要的所有数据,但是我想添加不是来自json的额外变量。

struct LoanUser: Codable {
let name: String
let use: String
let location: Location
let loan_amount: Int
let image: Image
var favorite: Bool = false 

} }

The var favorite: bool = false is not a json string. var最喜欢的:bool = false不是json字符串。 This is the extra variable I want added 这是我要添加的额外变量

You have to specify coding keys by yourself and do not include favorite 您必须自己指定编码键,并且不包括收藏夹

struct LoanUser: Codable {
    let name: String
    let use: String
    let location: Location
    let loan_amount: Int
    let image: Image
    var favorite: Bool = false

    enum CodingKeys: String, CodingKey {
        case name, use, location, loan_amount, image
    }
}

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

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