Dennis vd Eijnde Dennis vd Eijnde - 10 days ago 5x
iOS Question

Why do i get a empty Tableview with these code? JSON

i'm having some trouble with loading data into my tableview. The data doesn't seem to load properly. I'm just getting started to IOS programming, and have been stuck on this problem for a while.

Here is a screenshot of my storyboard

override func viewDidLoad() {

var afspraken = [Afspraak]()

func loadJsonData()
let url = URL(string: "")
let dataTask = URLSession.shared.dataTask(with: url!)
(data, response, error)
if error != nil
print (error)
let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .allowFragments)
self.parseJsonData(jsonObject as! Array<AnyObject>)
catch let error as NSError {print("Error parsing JSON: \(error)" )}


func parseJsonData(_ jsonObject: Array<AnyObject>) {
for item in jsonObject
let afspraak =
Afspraak(naam: item["naam"] as! String
, adres: item["adres"] as! String
, postcode: item["postcode"] as! String
, woonplaats: item["woonplaats"] as! String
, telefoonnummer: item["telefoonnummer"] as! String
, email: item["email"] as! String
, datum: item["datum"] as! Date
, tijdstip: item["tijdstip"] as! String
, omschrijving: item["omschrijving"] as! String)


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return afspraken.count

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let currentRow = indexPath.row
let currentAfspraak = self.afspraken[currentRow]
Cell.textLabel?.text = currentAfspraak.naam
return Cell


An URLDataTask Request is running in the background thread (non UI thread). Therefore you need to update the UI on the main thread using GCD-Grand Central Dispatch after completion.

DispatchQueue.main.sync {


I took a look into your JSON URL, it is not a JSON file, it is an HTML file. That's why the JSONSerialization is returning invalid. You need to put the .json file onto your server instead of embedding json string into an HTML file.

The file on your server is like this: as you can see this is an HTML.

as you can see this is an HTML.

The correct JSON file should be like this: enter image description here

Please notice the difference between the two URLs on top of the image, the correct one has .json, and yours is just /, which is a directory.

Therefore what you need to do is upload your JSON file to your server, and use the corresponding URL, like