I need help in decoding json data with Alamofire 5. I'm confused about this array which I cannot decode....
[
{
"Date": "2022-08-08T00:00:00",
"Day": "Fri",
"AbsenceCount": 0,
"AbsenceRequestComments": "Request permitted",
"Color": "No",
"IsRequestPermitted": true
},
{
"Date": "2022-09-09T00:00:00",
"Day": "Sat",
"AbsenceCount": 0,
"AbsenceRequestComments": "Request permitted",
"Color": "No",
"IsRequestPermitted": true
},
{
"Date": "2022-12-15T00:00:00",
"Day": "Sun",
"AbsenceCount": 0,
"AbsenceRequestComments": "Request permitted",
"Color": "No",
"IsRequestPermitted": true
}
]
I'm trying all from this stackoverflow and didn't find something helpful for my case. Also I tried responseString, responseData, responseJSON and finaly responseDecodableOf with Codable model.
let request = AF.request(requestURL, method: .get, parameters: parameters, encoding: URLEncoding.default, headers: returnHeader())
.validate()
.responseDecodable(of: [CoverCheckForDay].self) { response in
switch response.result {
case .success:
guard let value = response.value else { failure(""); return }
success(value)
case .failure(let error):
failure(error.localizedDescription)
}
}
My model
struct CoverCheckForDay: Codable {
let date: String
let day: String
let absenceCount: Int
let absenceRequestComments: String
let color: String
let isRequestPermitted: Bool
enum CodingKeys: String, CodingKey {
case date = "Date"
case day = "Day"
case absenceCount = "AbsenceCount"
case absenceRequestComments = "AbsenceRequestComments"
case color = "Color"
case isRequestPermitted = "IsRequestPermitted"
}
}
I'm getting errors of invalid type, also in case of responseString I had some string: "\" . That's all, I'll be very thankfull for your help, thanks!
EDIT: error from alamofire
▿ Optional<AFError>
▿ some : AFError
▿ responseSerializationFailed : 1 element
▿ reason : ResponseSerializationFailureReason
▿ decodingFailed : 1 element
▿ error : DecodingError
▿ typeMismatch : 2 elements
- .0 : Swift.Array<Any>
▿ .1 : Context
- codingPath : 0 elements
- debugDescription : "Expected to decode Array<Any> but found a string/data instead."
- underlyingError : nil
My parameters:
▿ 3 elements
▿ 0 : 2 elements
- key : "employeeId"
- value : 13558
▿ 1 : 2 elements
- key : "dates"
▿ value : 3 elements
▿ 0 : CoverRequestArray
▿ date : Optional<String>
- some : "01/09/2022"
▿ day : Optional<String>
- some : "Thu"
▿ 1 : CoverRequestArray
▿ date : Optional<String>
- some : "02/09/2022"
▿ day : Optional<String>
- some : "Fri"
▿ 2 : CoverRequestArray
▿ date : Optional<String>
- some : "03/09/2022"
▿ day : Optional<String>
- some : "Sat"
▿ 2 : 2 elements
- key : "siteId"
- value : 440
CodePudding user response:
Why don't you try decoding like this:
In your file but outside your struct add:
typealias CoverCheck = [CoverCheckForDay]
and decode like:
AF.request(requestURL, method: .get)
.response{ response in
switch response.result {
case .success(let data):
let newJSONDecoder = JSONDecoder()
if let result = try? newJSONDecoder.decode(CoverCheck.self, from: data!){
print(result)
}
case .failure(let error):
print(error)
}
}
CodePudding user response:
Your output is an empty string, your custom odd failure-value.
The problem is that the handling of the success
case is wrong.
.success
has a non-optional associated value like .failure
which is the decoded object.
Change the code to something like
.responseDecodable(of: [CoverCheckForDay].self) { response in
switch response.result {
case .success(let coverCheckArray):
success(coverCheckArray)