Learn Courses My Dashboard

Parsing JSON, how to test?

I’m trying out some JSON parsing following the method laid out in the Recipe App Module. There are no error messages occurring, however when I to bring data form the JSON into my View with a ForEach List, nothing shows up.

Before I paste the code up here and beg for help, does any one have any advice on how to test that the parsing is working?

Thanks,

James

If you are doing something like try? decoder.decode(...), don’t. That masks any errors that occur by just returning nil if something goes wrong. Instead use a construction like:

do {
    let response = try decoder.decode(...)
    //continue with the happy path
}
catch {
    print(error)
}

This will print an error to the debugger ouput so you can see what’s going wrong.

If you can’t figure out how to fix it from that, post your code here and we’ll take a look.

James,

There are some tools on line to verify that your JSON is valid. This is one of them: https://jsonlint.com

There is also a handy tool to paste JSON into and it will give you the structs required to set up your model.
https://app.quicktype.io

Thanks both. It turns out my parsing was fine, when I fired up Xcode the day after it was working as it should. I had tried closing and re-opening Xcode at the time but it appears it needed a little longer to think it over! Xcode seems to be very temperamental!

Sometimes you have to reboot your machine to get it to play the game.