I'm trying to use swift 4 to parse a local json file:
{
"success": true,
"lastId": null,
"hasMore": false,
"foundEndpoint": "https://endpoint",
"error": null
}
This is the function I'm using:
func loadLocalJSON() {
if let path = Bundle.main.path(forResource: "localJSON", ofType: "json") {
let url = URL(fileURLWithPath: path)
do {
let data = try Data(contentsOf: url)
let colors = try JSONDecoder().decode([String: Any].self, from: data)
print(colors)
}
catch { print("Local JSON not loaded")}
}
}
}
but I keep getting the error:
Fatal error: Dictionary does not conform to Decodable because Any does not conform to Decodable.
I tried using the "AnyDecodable" approach on this stackoverflow page: How to decode a property with type of JSON dictionary in Swift 4 decodable protocol
but it jumps to the 'catch' statement: catch { print("Local JSON not loaded")
when being used. Does anyone know how to parse this JSON data in Swift 4?