Я читаю содержимое файла JSON с этим кодом:
func loadJson(filename fileName: String) -> [categories]? {
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
do {
print("the url is",url.absoluteString)
let data = try Data(contentsOf: url, options: .mappedIfSafe)
print("the data content is \(data)")
let decoder = JSONDecoder()
let jsonData = try decoder.decode(ResponseData.self, from: data)
return jsonData.person
} catch {
print("error:\(error)")
}
}
return nil
}
Содержимое файла выглядит следующим образом:
[
{
"name": "Telefonía",
"icon": "mobile",
"id": "5b4dd0a69a38237f574de754",
"url": "https://es.trueketeke.io/anuncios-segunda-mano-categoria/telefonia/5b4dd0a69a38237f574de754",
"children": [
{
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles",
"children": [
{
"name": "Servicios de telefonía",
"id": "5b4dd0a69a38237f574de756",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.327Z",
"updatedAt": "2018-07-17T11:19:02.327Z",
"nested": "Moviles > Servicios de telefonía",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Manos libres",
"id": "5b4dd0a69a38237f574de757",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.416Z",
"updatedAt": "2018-07-17T11:19:02.416Z",
"nested": "Moviles > Manos libres",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Fundas",
"id": "5b4dd0a69a38237f574de758",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.505Z",
"updatedAt": "2018-07-17T11:19:02.505Z",
"nested": "Moviles > Fundas",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Tarjetas prepago",
"id": "5b4dd0a69a38237f574de759",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.606Z",
"updatedAt": "2018-07-17T11:19:02.606Z",
"nested": "Moviles > Tarjetas prepago",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Cables",
"id": "5b4dd0a69a38237f574de75a",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.687Z",
"updatedAt": "2018-07-17T11:19:02.687Z",
"nested": "Moviles > Cables",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Cargadores",
"id": "5b4dd0a69a38237f574de75b",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.784Z",
"updatedAt": "2018-07-17T11:19:02.784Z",
"nested": "Moviles > Cargadores",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
},
{
"name": "Baterias",
"id": "5b4dd0a69a38237f574de75c",
"categoryId": "5b4dd0a69a38237f574de754",
"subcategoryId": "5b4dd0a69a38237f574de755",
"createdAt": "2018-07-17T11:19:02.872Z",
"updatedAt": "2018-07-17T11:19:02.872Z",
"nested": "Moviles > Baterias",
"subcategory": {
"name": "Moviles",
"id": "5b4dd0a69a38237f574de755",
"categoryId": "5b4dd0a69a38237f574de754",
"createdAt": "2018-07-17T11:19:02.231Z",
"updatedAt": "2018-07-17T11:19:02.231Z",
"nested": "Moviles"
}
}
]
}
]
Содержимое файла JSON значительнобольше, чем это.Я отрезал для удобства чтения.Дело в том, что чтение не удается с этим:
ошибка: typeMismatch (Swift.Dictionary, Swift.DecodingError.Context (codingPath: [], debugDescription: «Ожидается декодирование словаря»но вместо этого нашел массив. ",ringError: nil))
Я полагаю, что данные ожидают разногласий, но поскольку это массив, то он не выполняется.