我一直在尝试使用alamofire从url获得json响应。创建了模型,apirouter和api客户端类。
显示错误
failure(Alamofire.AFError.responseSerializationFailed(reason:
Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error:
Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "variables",intvalue: nil),Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0",intvalue: 0)],debugDescription: "No value associated with key CodingKeys(stringValue: \"variables\",intvalue: nil) (\"variables\").",underlyingError: nil)))))
这是我的邮递员json回复:
[
{
"id": "00602c70-fc8a-11e9-ad1d-2abe2670111d","resourceType": "Task","name": "My Tasks","owner": null,"query": {
"assigneeExpression": "${currentUser()}","taskVariables": [],"processVariables": [],"caseInstanceVariables": [],"orQueries": []
},"properties": {
"variables": [
{
"name": "loanAmount","label": "Loan Amount"
},{
"name": "firstName","label": "First Name"
}
],"color": "#555555","showUndefinedVariable": false,"description": "Tasks assigned to me","refresh": false,"priority": -10
}
}
]
我试图从json响应中获取 id,名称和属性->变量->名称和标签的值。
这是模型类别:
import Foundation
public struct Filter: Codable {
let id: String
let name: String
let properties: [variables]
}
public struct variables: Codable {
let name: String
let label: String
}
以下是alamofire的代码:
private static func performRequest<T:Decodable>(route:APIRouter,decoder: JSONDecoder = JSONDecoder(),completion:@escaping (AFResult<T>)->Void) -> DataRequest {
return AF.request(route)
.responseDecodable (decoder: decoder){ (response: AFDataResponse<T>) in
completion(response.result)
print("framework response::",response.result)
}
}
public static func getFilter(completion:@escaping (AFResult<[Filter]>)->Void) {
let jsonDecoder = JSONDecoder()
performRequest(route: APIRouter.getFilter,decoder: jsonDecoder,completion: completion)
}
任何帮助都非常感谢...