【问题标题】:How to get response in responseDecodable in Alamofire 5如何在 Alamofire 5 中的 responseDecodable 中获得响应
【发布时间】:2021-03-13 03:10:12
【问题描述】:

我从 API 收到两个响应。
在失败的情况下

{"message":"Either your supplied Email ID or Password is incorrect","status":201,"data":{}}

成功案例

{
    "status": 200,
    "data": {
        "access_token": "7a1bfe8c85ef81c635d7d14b9e335984",
        "app_access_token": "168aa07e4966186447694e95e4127fb5",
       }
}

我正在使用带有可选的 decodeIfPresent 将数据解析为单个结构。有没有更好的方法来解析数据而不可选地使用失败结构和成功结构?
任何指导总是值得赞赏的

【问题讨论】:

    标签: ios json swift alamofire


    【解决方案1】:

    您可以使用 swiftJSON pod 并在 JSON 中解析数据,这是我为我的 api 响应所做的,并在 json 中处理它,您可以按照自己的方式使用和编辑

    func ProfileApi(url : String , parameters : [String : String]){
        
        Alamofire.request(url , method : .post , parameters : parameters).responseJSON { (response) in
            if response.result.isSuccess
            {
                print("API data recived")
                let DiseaseCatagory : JSON = JSON(response.result.value!)
                // print(plotCatagory[0]["name"])
                
                //self.SelectedLevelIDArray = []
                if let data = DiseaseCatagory.dictionaryObject
                {
                    if let error = data["error_code"] as? Int{
                        print(error)
                        if error == 0{
                            
                            if let data1 = data["data"] as? [String : Any]
                            {
                                print(data1)
                                if let PostDataRecieved = data1["about"] as? [[String : Any]]
                                {
                                    print(PostDataRecieved)
                                    var i = 0
                                    
                                    while(i < PostDataRecieved.count){
                                        
                                        self.ProfileName = PostDataRecieved[i]["first_name"] as! String
                                        self.ProfileGender = PostDataRecieved[i]["gender"] as! String
                                        self.ProfileEmail = PostDataRecieved[i]["email"] as! String
                                        self.ProfileCountry = PostDataRecieved[i]["country"] as! String
                                        self.ProfileDOB = PostDataRecieved[i]["dob"] as! String
                                        self.profileImageUrl = PostDataRecieved[i]["profile_picture"] as! String
                                        if let lastName = PostDataRecieved[i]["last_name"] as? String{
                                            
                                            self.profileLastName = lastName
                                            self.LastNameTextField.text = lastName
                                        }
                                        
                                        
                                        
                                        
                                        
                                        i = i + 1
                                    }
                                }
                                self.AssignValues()
                                DispatchQueue.main.async {
                                    
                                    self.hud.dismiss(afterDelay: 0.5)
                                    
                                }
                                
                            }
                        }else{
                            if let error = data["error_string"] as? String{
                                let signUpAlert = UIAlertController(title: "Signup Error!", message: error, preferredStyle: .alert)
                                let okButton = UIAlertAction(title: "ok", style: .default, handler: nil)
                                signUpAlert.addAction(okButton)
                                self.present(signUpAlert, animated: true, completion: nil)
                            }
                            DispatchQueue.main.async {
                                
                                self.hud.dismiss(afterDelay: 0.5)
                                
                            }
                            
                            
                        }
                        
                    }
                    
                }
                
            }
            else
            {
                
                print("Error : \(response.result.error)")
                let signUpAlert = UIAlertController(title: "Connection Error!", message: "Please check internet connection and try again", preferredStyle: .alert)
                let okButton = UIAlertAction(title: "ok", style: .default, handler: nil)
                signUpAlert.addAction(okButton)
                self.present(signUpAlert, animated: true, completion: nil)
                
            }
            
        }
    }
    

    这是我正在处理的 JSON 响应

    {"error_code":0,"error_string":"","data":{"about":[{"id":"2","user_name":"","first_name":"Mazeda","last_name":"Khanam","mobile_no":"07387276289","email":"mazedakhanam@yahoo.co.uk","profile_picture":"user_icon.jpeg","gender":"female","profession":"Mum","dob":"1984-09-06","mood_status":"Hey There!","registered_time":"0000-00-00 00:00:00","account_verified":"1","login_flag":"1","country":"United Kingdom","added_time":"2017-02-23 15:42:54","status":"1","android_push_token":"","ios_push_token":""}],"photo":[]}}
    

    【讨论】:

    • 不想像 swiftJSON 一样使用第三方库。想在 Codable 协议中处理。
    • 它是我遇到的用于处理 JSON 的最优雅的库,没有副作用
    • 赞赏,但 Codable 是 Swift 提供的本机功能。为什么我要使用第三方库而不是原生功能?
    猜你喜欢
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多