SWift 4 JSONDecoder 从 Alamofire 响应解码

SWift 4 JSONDecoder decode from Alamofire response

我有一个包含另一个结构和数组的结构。

public struct Report: Codable {
 let s:Student;
 let vio:[VIO];
 let  stuPresence: [StuPresence]; 
}

我正在尝试新 JSONDecoder() 将 alamofire 响应转换为我的结构。

sessionManager.request( self.url_report+"?d="+date, method: .get, parameters: nil).responseJSON{ response in
    if response.response?.statusCode == 200 {
            debugPrint(response)
            do{
                let r = try JSONDecoder().decode(Report.self, from: response.result.value as! Data)
                debugPrint(r);
            }catch{
               self.showMessage(message: self.general_err)
            }
    }
}

问题是在我的 Report 结构中解码后得到的不是字符串,而是数字(从调试模式检查)。我究竟做错了什么?

更新:它也给出了错误

Could not cast value of type '__NSDictionaryI' (0x108011508) to 'NSData' (0x108010090)

错误很明显:

response.result.value 显然是一个不能转换为 (NS)Data 的字典 (__NSDictionaryI)。这意味着 JSON 已经被反序列化。

为了能够使用 JSONDecoder,您必须将 Alamofire 设置更改为 return raw Data