如何处理 ios swift 4 中的 Alamofire 响应?
How to handle Alamofire Response in ios swift 4?
我能够使用 Alamofire 集成 POST 请求。我正在获取以下响应。但我需要将“docname”关键文档添加到数组中。能否请任何人指导我处理此回复
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"
}
]
}
}
}
我的源代码:
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
// 打印(响应)
switch response.result {
case .success(let response):
//Sucess
// self.progress.hide()
// print("response :-", response)
let json = response as? [String: Any]
// 打印(json!)
self.status = json!["Status"] as? String
print(self.status)
case .failure(let error):
//Failure
print(error.localizedDescription)
// self.progress.hide()
}
}
}
}
如下所示为响应创建数据类型 json 并使用 JSONDecoder 解析响应。
// 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 { [=10=].docname })
} catch {
print(error)
}
case .failure(let error):
print(error)
}
}
我能够使用 Alamofire 集成 POST 请求。我正在获取以下响应。但我需要将“docname”关键文档添加到数组中。能否请任何人指导我处理此回复
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"
}
]
}
}
}
我的源代码:
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
// 打印(响应)
switch response.result {
case .success(let response):
//Sucess
// self.progress.hide()
// print("response :-", response)
let json = response as? [String: Any]
// 打印(json!)
self.status = json!["Status"] as? String
print(self.status)
case .failure(let error):
//Failure
print(error.localizedDescription)
// self.progress.hide()
}
}
}
}
如下所示为响应创建数据类型 json 并使用 JSONDecoder 解析响应。
// 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 { [=10=].docname })
} catch {
print(error)
}
case .failure(let error):
print(error)
}
}