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
So guys problem was in server side with wrong method I guess. Backend developer from side did GET method with input data body. Web says that from 2019 Apple violate send body data with GET.
Thanks everyone for your answers, so in some occasional it's my failure that I didn't checked this first.
Screen from postman: