简体   繁体   中英

how to get the response JSON in swift

I got a response from my Alamofire post request. I want to get the Status code from the response. Here in the code:

case .success(let upload, _, _):
    upload.responseJSON { response in
        print(response.result.value!)
    }

Here is the response looks like:

响应图像

The value property of the response's result is a dictionary. You can extract status like this:

let json = response.result.value as? [String: Any]
if let status = json?["status"] as? Int {
    print(status)
}

Use responseDecodable in Alamofire 5 with Codable Structure

Replace responseJSON with responseDecodable.
struct ProfileModel: Codable {
    let status: Int
    let type: String
    let data: ProfileDataModel
}

struct ProfileDataModel: Codable {
    let ImagePath: String
    let ThumbImagePath: String
}

ProfileModel

struct ProfileModel: Codable { let status: Int let type: String let data: ProfileDataModel } struct ProfileDataModel: Codable { let ImagePath: String let ThumbImagePath: String }

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