【问题标题】:how to get the response JSON in swift如何快速获取响应 JSON
【发布时间】:2020-11-29 10:01:28
【问题描述】:

我收到了来自 Alamofire 发布请求的回复。我想从响应中获取状态码。 代码如下:

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

下面是响应:

【问题讨论】:

    标签: ios swift alamofire


    【解决方案1】:

    响应结果的value 属性是一个字典。您可以像这样提取状态:

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

    【讨论】:

      【解决方案2】:

      在具有可编码结构的 Alamofire 5 中使用 responseDecodable

      将 responseJSON 替换为 responseDecodable。
      .responseDecodable { (response: AFDataResponse<ProfileModel>) in
              switch response.result {
              case .success(let profileModel):
                  switch profileModel.status {
                  case 200:
                      print("success")
                  case 101:
                      print("sessionExpire")
                  default:
                      print("default")
                  }
              case .failure(let error):
                  print("failure")
              }
          }
      

      个人资料模型

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

      【讨论】:

        猜你喜欢
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        • 1970-01-01
        • 2020-11-28
        • 2015-10-17
        • 1970-01-01
        相关资源
        最近更新 更多