Reputation: 125
I have been trying to get json response from url using alamofire. Created model, apirouter and api client class.
its shows error
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)))))
Here is my postman json response:
[
{
"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
}
}
]
My trying get values for id, name and properties -> variables -> name and label from json response.
Here is model class:
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
}
Here is code for 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)
}
Any help much appreciates pls...
Upvotes: 1
Views: 3266
Reputation: 1137
The error message you're getting is pretty clear: No value associated with key CodingKeys(stringValue: \"variables\"
You're attempting to decode the JSON into the Filter
struct, but the JSON has no variables
property. You could fix this by introducing a new Properties
struct that will wrap the variables
property, like this:
struct Filter: Codable {
let id: String
let name: String
let properties: Properties
}
struct Properties: Codable {
let variables: Variables
}
struct Variables: Codable {
let name: String
let label: String
}
Also, as you'll see in this snippet, it's convention to make the write the type names in CamelCase, so struct Variables
instead of struct variables
.
Upvotes: 0
Reputation: 195
Did you try this one? This is what it should be
public struct Filter: Codable {
let id: String
let name: String
let properties: Property
}
public struct Property: Codable {
let variables: [Variable]
}
public struct Variable: Codable {
let name: String
let label: String
}
Upvotes: 1
Reputation: 3930
your model class should be like the below.
import Foundation
public struct Filter: Codable {
let id: String
let name: String
let properties: Properties
}
public struct Properties: Codable {
let variables: [variables]
let color: String
let showUndefinedVariable: Bool
let description: String
let refresh: Bool
let priority: Int
}
public struct variables: Codable {
let name: String
let label: String
}
Upvotes: 3