簡體   English   中英

如何在 swift 中獲得響應 JSON

[英]how to get the response JSON in swift

我收到了 Alamofire 發布請求的回復。 我想從響應中獲取狀態碼。 在代碼中:

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

這是響應的樣子:

響應圖像

響應結果的value屬性是一個字典。 您可以像這樣提取狀態:

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

在具有可編碼結構的 Alamofire 5 中使用 responseDecodable

將 responseJSON 替換為 responseDecodable。
struct ProfileModel: Codable {
    let status: Int
    let type: String
    let data: ProfileDataModel
}

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

輪廓模型

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM