kodeco.com Forums

Swift Tutorial: Working with JSON

In this tutorial, you'll learn how to process JSON in Swift 2, both natively and using the fantastic Gloss framework. Developers, start your parsers!

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1374-swift-tutorial-working-with-json

@cynicalme, Thank you for an excellent tutorial. As suggested I applied the Gloss library to an OpenWeatherMap forecast example and it worked well and first time.

Thanks Attila, amazing job and tutorial! Through this tutorial I gained a good kick-off how to use JSON in Swift. On top of that, it was a good practice for me to perform this tutorial with the new API and implement it right away in Swift 3.0. Luckily Gloss has a branch updated already which I could use :slight_smile:
You can take a look at my code if you like here, it can be useful when you are upgrading the tutorial to the new API :slight_smile:

One more comment: instead of “playground” you were consistently using “storyboard” in the second part of the tutorial, I guess that was not on purpose :slight_smile:

do {
json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? Payload
} catch {
I am not sure weather it is do while loop or something else? Could you please explain me what actully is going on here? i am getting bit confused :confused: with the braces and do{}.

The do-catch statement is used to handle errors. Here is a snippet from the Swift docs.

Handling Errors Using Do-Catch

You use a do-catch statement to handle errors by running a block of code. If an error is thrown by the code in the do clause, it is matched against the catch clauses to determine which one of them can handle the error.

In the code you listed, any error in the “json = try …” statement would result in the code in the catch block being executed.

Hope that helps!

I am getting error “Use of undeclared type Date”. Please suggest me what to do, bcz I am new with Swift.

Great tutorial, but can you please update the tutorial for Swift 3 ?
and if there is a tutorial to use gloss with simpler JSON code I’ll be grateful.

Thanks for the tutorial.