简体   繁体   English

如何在 Alamofire 5 中的 responseDecodable 中获得响应

[英]How to get response in responseDecodable in Alamofire 5

I am getting two response from API.我收到了来自 API 的两个回复。
In failure case在失败的情况下

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

In success case在成功案例中

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

I am parsing the data into a single structure using decodeIfPresent with optional.我正在使用可选的decodeIfPresent将数据解析为单个结构。 Is there any better way to parse the data without optional using failure structure and success structure?有没有更好的方法来解析数据而无需使用失败结构和成功结构?
Any guidance always be appreciated任何指导总是值得赞赏

you can use swiftJSON pod and parse data in JSON this is what i do for my api responce and handle it in json you can use and edit in your way您可以使用 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)
            
        }
        
    }
}

this the JSON responce which i am handling这是我正在处理的 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":[]}}

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

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