Learn Courses My Dashboard

Parsing JSON objects

Hello, I would like to display all backupId objects in a tableview. I finished Full Stack Notes app module, so I know how to parse non-nested JSON objects, but not nested ones. I would also like to parse only backupId objects and not the other ones, as I don’t want to display them. Any help is very much appreciated!

If you paste your json code into this web based app you will get a suggested data model that you can use.

https://app.quicktype.io

I tried that, but it still fails do decode for some reason. Could you please fix my code so that it will display backupId-s in a tableview? I fetch JSON using Minecraft Bedrock Realm API, so I added a local JSON file. Here’s the link to the project: https://drive.google.com/file/d/1tu4oJEuBNNBJ8RF_KEEo6-ITUnVBUze8/view?usp=sharing
Thanks a lot!

@SplashFruit

Hi Tarik,

I’ve had a look at your project and the first problem I found was that the Alamofire request was returning response.data as nil so it just crashes. That needs to be resolved and I can’t help you there unfortunately since I do not have the experience that Chris Ching has. Maybe someone else will chime in and advise.

I created a localJSON function to decode the JSON code you already have in the Bundle and present that in the View Controller. That works.

The function I added is getLocalJSON()

Looking at your model, what you are interested in is the backups so what you need to declare is an array of Backup.

var backupArray = [Backup]()

Your DataDelegate should now return an array of [Backup] which makes more sense I believe.

I commented out the JSON decoding in the function updateArray() since the local json decoding already supplies an array of [Backup].

In MCFunctions.swift file I cleaned up the model by changing metadataDescription to an Optional String which means that the confusing and unnecessary JSONNull class can be removed. It just makes things cleaner.

I placed some note in your fetchBackups() regarding response.data being nil. Like I say, you will have to figure out that for yourself.

Link to modified project: Dropbox - ModifiedProject.zip - Simplify your life