我正在尝试在我的应用程序中解析嵌套的json,但是我一直收到一个我不理解的错误。使用普通的json,它完美无缺。以下是我对数据进行建模的方法:
import Foundation
struct Producer: Decodable {
var data: ProducerData?
var messages: [String]?
var success: Int?
}
struct ProducerData : Decodable {
let id: Int?
let name: String?
let login: String?
let producer: EventProducer?
let roles: [String]?
let config: [String]?
}
struct EventProducer : Decodable {
let id: Int?
let name: String?
}
这是json的请求和解码:
Alamofire.request(url, headers: headers).responseJSON {
response in
switch response.result {
case .success:
print(response)
do {
response.result.value
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
self.producer = try jsonDecoder.decode(Producer.self, from: response.data!)
} catch {
print(error)
}
break
case .failure(let error):
print(error)
}
}
我得到的错误是:
typeMismatch(Swift.Int,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue:“data”,intValue:nil),CodingKeys(stringValue:“id”,intValue:nil)],debugDescription:“预计到解码Int但是找到了一个字符串/数据。“,underlyingError:nil))
任何帮助都会很棒!