I've been trying to parse this object from my JSON
and keep getting this error:
"Error: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))\n"
If I remove Array Bracket from here let video = try decoder.decode([Content].self, from: data)
then get an error saying:
"Error: keyNotFound(CodingKeys(stringValue: "description", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"description\", intValue: nil) (\"description\").", underlyingError: nil))\n"
How can I get this to go away? Here is my JSON
and code:
JSON:
> { "content": [{
> "description": "Hello",
> "category": "World wides",
> "creator": {
> "name": "The One",
> "site": "Purple",
> "url": "http://www.sample.com"
> },
> "time": 300,
> "full": "https:sample2.com",
> "clothes": "jacket",
> }]
}
struct Content: Decodable {
let description: String
let category: String
}
if let fileURL = Bundle.main.url(forResource: "stub", withExtension: "json") {
do {
let data = try Data(contentsOf: fileURL)
let decoder = JSONDecoder()
let video = try decoder.decode([Content].self, from: data)
print(video.description)
// Success!
// print(content.category)
} catch {
print("Error: \(error)")
}
} else {
print("No such file URL.")
}
let video = try decoder.decode([Content].self, from: data)
tolet video = try decoder.decode(Content.self, from: data)
– Hitesh Agarwal