The problem with the code you have is that you are trying to convert an array to a dictionary (as rightly pointed out by a couple of people in the comments Vadian, Moritz at the time of writing).
So the obvious, first step solution would be instead of casting to [String: Any]
cast to [[String: Any]]
(an array of dictionaries) but this then still leaves you with the problem of the Any
part. To work with this dictionary you need to know/remember the keys and their types and cast each value as you use it.
It is much better to use the Codable
protocol which along with the decoder allows you to basically map the JSON to related code structures.
Here is an example of how you could parse this JSON using the Swift 4 codable protocol (done in a Swift Playground)
let jsonData = """
[{
"idcatalog": 1,
"imgbase64": "",
"urlImg": "http://172.../page01.jpg",
"urlPages": "http://172.../catalog_1_pages.json",
"dt_from": "",
"dt_to": ""
}, {
"idcatalog": 2,
"imgbase64": "",
"urlImg": "http://172.../1.jpg",
"urlPages": "http://172.../2_pages.json",
"category": [{
"id": 1,
"lib": "lib"
}],
"dt_to": ""
}]
""".data(using: .utf8)!
struct CatalogItem: Codable {
let idCatalog: Int
let imgBase64: String?
let urlImg: URL
let urlPages: URL
let dtFrom: String?
let dtTo: String?
let category: [Category]?
enum CodingKeys: String, CodingKey {
case idCatalog = "idcatalog"
case imgBase64 = "imgbase64"
case urlImg, urlPages, category
case dtFrom = "dt_from"
case dtTo = "dt_to"
}
}
struct Category: Codable {
let id: Int
let lib: String?
enum CodingKeys: String, CodingKey {
case id, lib
}
}
do {
let decoder = JSONDecoder()
let result = try decoder.decode([CatalogItem].self, from: jsonData)
print(result)
} catch {
print(error)
}
Console output:
[__lldb_expr_118.CatalogItem(idCatalog: 1, imgBase64: Optional(""), urlImg: http://172.../page01.jpg, urlPages: http://172.../catalog_1_pages.json, dtFrom: Optional(""), dtTo: Optional(""), category: nil), __lldb_expr_118.CatalogItem(idCatalog: 2, imgBase64: Optional(""), urlImg: http://172.../1.jpg, urlPages: http://172.../2_pages.json, dtFrom: nil, dtTo: Optional(""), category: Optional([__lldb_expr_118.Category(id: 1, lib: Optional("lib"))]))]
Now comes the good part, it's much easier to use this data now...
print(result.first?.urlImg)
Output:
Optional(http://172.../page01.jpg)
This works for the example JSON you provided but may need more tweaks based on the rest of your data set.