简体   繁体   中英

how to send nil when there is no value for a field in json?

email and a_code are optional. i do want to send nil when they are empty. should i check and if they were nil ,remove them from json?

this is the body for json i want to send

 func updateProfile(fullName: String,address: String,homePhone: String,email: String, a_code: String, completion : @escaping CompletionHandler) {
        let body : [String : Any] = [
            "name": fullName,
            "address": address,
            "h_phone": homePhone,
            "email": email,
            "a_code": a_code
        ]

        Alamofire.request(UPDATE_PROFILE_FIRST, method: .post, parameters: body, encoding: JSONEncoding.default, headers: AUTH_HEADER).validate().responseJSON { (response) in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
                print(json)
                let s = json["s"].intValue
                if s == 24 {
                    completion(true)
                }else {
                    completion(false)
                }

            case .failure(let error):
                print(error.localizedDescription)
                completion(false)
            }
        }
    }
    func updateProfile(fullName: String,address: String,homePhone: String,email: String, a_code: String, completion : @escaping CompletionHandler) {
            let body : [String : Any] = [
                "name": fullName,
                "address": address,
                "h_phone": homePhone,
                "email": email.isEmpty ? nil : email ,
                "a_code": a_code.isEmpty ? nil : a_code
            ]
            ...
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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