1

I am able to integrate POST request using Alamofire. I am fetching following response. but I need to add "docname" key docs into array. Could you please anyone guide me to handle this response

My Response:

{
    "ResponsePayload": {
        "StatusData": {
            "StatusCode": "200",
            "Status": "SUCCESS",
            "Documents": [
                {
                    "docname": "Standard",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard2",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard3",
                    "isUploaded": "false"
                },
                {
                    "docname": "Standard4",
                    "isUploaded": "false"
                },
                {
                    "docname": "Standard5",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "false"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "true"
                },
                {
                    "docname": "DNC Format",
                    "isUploaded": "true"
                },
                {
                    "docname": "Form 60",
                    "isUploaded": "false"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "true"
                },
                {
                    "docname": "Standard",
                    "isUploaded": "false"
                }
            ]
        }
    }
}

My Source code:

func getMandatoryDocs(){

    print("calling ")
    
let url = "url"
    
    
    let headers = ["Content-Type": "application/json"]

    
    let parameters: Parameters = [
        "Request": [
            "RequestPayload": [
                "ProposalId": self.textField?.text!,
                "Type": "getMandatoryDocuments"
            ]
        ]
    ]
    
    
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in
            if response.result.isFailure {
                            //In case of failure
                }else {
                            //in case of success

// print(response)

                            switch response.result {
                            case .success(let response):
                                //Sucess
                                //                        self.progress.hide()
                                //                            print("response :-", response)
        
                                let json = response as? [String: Any]

// print(json!)

                                self.status = json!["Status"] as? String
        
                                print(self.status)
                
                            case .failure(let error):
                                //Failure
                                print(error.localizedDescription)
                                //                        self.progress.hide()
                            }
        
        
        
        
        
                        }
                    }
    
}
Gopal
  • 35
  • 4

1 Answers1

1

Create data types for the response json as below and use JSONDecoder to parse the response.

// MARK: - PostBody
struct PostBody: Codable {
    let responsePayload: ResponsePayload

    enum CodingKeys: String, CodingKey {
        case responsePayload = "ResponsePayload"
    }
}

// MARK: - ResponsePayload
struct ResponsePayload: Codable {
    let statusData: StatusData

    enum CodingKeys: String, CodingKey {
        case statusData = "StatusData"
    }
}

// MARK: - StatusData
struct StatusData: Codable {
    let statusCode: String
    let status: String
    let documents: [Document]

    enum CodingKeys: String, CodingKey {
        case statusCode = "StatusCode"
        case status = "Status"
        case documents = "Documents"
    }
}

// MARK: - Document
struct Document: Codable {
    let docname: String
    let isUploaded: String

    enum CodingKeys: String, CodingKey {
        case docname = "docname"
        case isUploaded = "isUploaded"
    }
}

AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseData { response in
    switch response.result {
    case .success(let data):
        do {
            let postBody = try JSONDecoder().decode(PostBody.self, from: data)
            print(postBody.responsePayload.statusData.statusCode)
            print(postBody.responsePayload.statusData.documents.map { $0.docname })
         } catch {
             print(error)
         }
    case .failure(let error):
        print(error)
    }
}
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • This Should be the accepted answer. Could you please tell me How to "docname" will append to an array? – Gopal Aug 13 '20 at 09:17
  • @Gopal, didn't understand the question. Do you want to add docname to some other Array of Strings? Or you want to create an Array of docnames only? – Kamran Aug 13 '20 at 09:23
  • I want to create an array of docnames only – Gopal Aug 13 '20 at 09:28
  • @Gopal Please see the edit. I have printed the list of docnames above. – Kamran Aug 13 '20 at 10:58