I'm trying to parse a JSON file with the following format:
{
"OnDemand" : {
"program" : [
{
"Sunday" : "https://example1.m4a",
"SundaySharePage" : "https://example1",
"name" : "Example 1",
"weekdays" : "Sunday"
},
{
"Monday" : "https://example2.m4a",
"MondaySharePage" : "https://example2",
"name" : "Example 2",
"weekdays" : "Monday"
}
]
}
Using this code:
struct AllPrograms: Codable {
let OnDemand: [Dictionary<String,String>: Programs]
}
struct Programs: Codable {
let program:Array<String>
}
func parsePrograms (urlString:String) {
if let url = URL(string: urlString) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let jsonDecoder = JSONDecoder()
do {
let parsedJSON = try jsonDecoder.decode(AllPrograms.self, from: data)
print(parsedJSON.OnDemand)
} catch {
print(error)
}
}
}.resume()
}
}
But I'm getting this error:
typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "OnDemand", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))
How do I fix this? Thank you!