0

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

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)
4

1 回答 1

2

错误很明显:

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

为了能够使用JSONDecoder,您必须更改Alamofire设置以返回原始Data

于 2018-04-01T11:15:48.500 回答