'Parsing JSON in Swift 5
I'm trying to parse a JSON file in Swift But I'm getting this error:
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "OnDemandJSONURLs", intValue: nil), _JSONKey(stringValue: "English", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
Here is my code:
struct Entry: Codable {
let OnDemandJSONURLs: [String: URLS]
}
struct URLS: Codable {
let English: String
let Korean: String
let ChineseSimplified: String
let German: String
let Russian: String
let Portuguese: String
let Japanese: String
let Spanish: String
let ChineseTraditional:String
}
func parseJSON() {
if let url = URL(string: JSON_URLS) {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let jsonDecoder = JSONDecoder()
do {
let parsedJSON = try jsonDecoder.decode(Entry.self, from: data)
print(parsedJSON)
} catch {
print(error)
}
}
}.resume()
}
}
The JSON file is:
{
"OnDemandJSONURLs": {
"English": "https://....json",
"Korean": "https://....json",
"ChineseSimplified":"https://....json",
"German": "https://....json",
"Russian": "https://....json",
"Portuguese": "https://....json",
"Japanese": "https://...json",
"Spanish": "https://....json",
"ChineseTraditional": "https://....json"
}
}
What Am I doing wrong? Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
