Как запарсить вложенный массив json swift 4


#1

Здравствуйте, мне нужно запарсить json:
12
в tableview, я сделал так:

struct News_data: Decodable {
let data: [Article_Data]

enum CodingKeys : String, CodingKey {
case data = "data"
}
}

struct Article_Data: Decodable {
let title: String
enum CodingKeys : String, CodingKey {
case title = "title"
}

}
 
var arrData = [Article_Data]()

func getCoinData() {
let jsonURL = "путь апи"
guard let url = URL(string: jsonURL) else { return }

URLSession.shared.dataTask(with: url) { [unowned self] (data, response, error) in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let data_new = try decoder.decode(News_data.self, from: data)

DispatchQueue.main.async {
self.arrData = data_new.data
self.tableview.reloadData()
}

} catch {
print("Error is : \n\(error)")
}
}.resume()
}
 
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
getCoinData()
}
 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return arrData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! MainTableViewCell
let dn = arrData[indexPath.row]
cell.textlabel.text = dn.title

Но я не могу обратится к массиву “cat”, скажите пожалуйста как я могу обратиться к нему
Заранеее благодарен


#2

Мыслите в правильном направлении. Но попробуйте подумать над своим кодом. Решение на поверхности, просто не делайте тупой копипаст, а по шагам пройдите в отладчике и посмотрите где у вас данные не приходят


#3

#4

cat у вас не массив.