I need to get data from an specific API which is formatted like:
{
"dates": {
"2021-10-01": {....},
"2021-10-02": {....},
"2021-10-03": {....},
"2021-10-04": {....},
"2021-10-05": {....},
......
......
and I need to access this data:
{
"dates": {
"2021-10-01": {
"countries": {
"Spain": {
"date": "2021-10-01",
"id": "spain",
"name": "Spain",
"name_es": "España",
"name_it": "Spagna",
"regions": [
{
"date": "2021-10-01",
"id": "andalucia",
"name": "Andalucía",
"name_es": "Andalucía",
"name_it": "Andalucía",
"source": "John Hopkins University",
So, I have implemented the next structures to Decode the data using JSONDecoder().decode()
:
struct AutonomousComunityDataResponse: Decodable {
let dates : datesData
}
struct datesData: Decodable {
let date : spain
}
struct spain: Decodable {
let countries : country2
}
struct country2: Decodable {
let Spain : regionsSpain
}
struct regionsSpain: Decodable {
let regions : [regionSpain]
}
struct regionSpain: Decodable {
let date: String
let today_new_confirmed: Int
}
I am getting an error when decoding the data because
keyNotFound(CodingKeys(stringValue: "date", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "dates", intValue: nil)],
debugDescription: "No value associated with key CodingKeys(stringValue: \"date\", intValue: nil) (\"date\").", underlyingError: nil))
I don't know how to implement this to get the data. I have thought something like a loop and just adding 1 to a variable like: 2020-10-01+1.
I am stuck in this part.