Hey everyone!
The last swift video seemed to answer questions for many of you, but is also sparked more
questions.
When we were posting JSON to our endpoint, I kinda glossed over how we read JSON using
Swift, so in this tutorial I figured we'd focus just on reading JSON.
This question was posted in the comments for the last tutorial, and I figured it would
be a great place to start.
Thanks Techie Stewie Stuff for the question.
Basically Techie is asking how to navigate the arrays and dictionaries when you load
JSON.
And thanks to Techie, we'll work with the JSON from the original question.
I'm putting the JSON into a file in the example code, but the real starting point for reading
JSON is NSData.
We'll start with NSData if it's coming from a URL as well.
JSON code can have three possibilities.
We might get back an array of items, we might get back key-value pairs, or we might get
a value.
Things between square braces are arrays, and curly braces are key-value pairs.
When we read JSON in Swift, we need to account for all possibilities.
An array gets loaded into an NSArray, and key-value pairs go into an NSDictionary.
Values can go into a number or
a string.
In our example JSON, our root has a single key value pair and it starts with a curly
brace.
That makes it a dictionary.
The root is an implied key, and the body of JSON document is the value.
We can read this all into one dictionary with the following line of code.
This parses the JSON into an NSDictionary.
If we didn't know the structure in advance, we'd need to check if our returned JSON object
was an array or dictionary.
We know it's a dictionary, so we'll just read it as such.
This gives us a dictionary with a single key-value pair.
The key is list, and everything after is the value.
So let's get the dictionary the key "list" points to.
We'll create another variable that gets the value for the key "list".
We'll cast the result as a dictionary.
Again, if we didn't know in advance what we were reading, we'd need to check to see if
the value is a dictionary, array, or string value.
Here's where things get a bit interesting.
Inside the list element, we have key value pairs where some of the objects are values,
and one is an array.
To read the total value, we dereference the value using the key.
The values are all optionals, so we'll get the value using the ! operator.
Getting the item element will return an array.
We can get the array the same way we got the total value.
In this case, we'll cast the result to an NSArray.
Once we have the array, we can loop and print each one.
Remember each item will always be an optional.
This code assumes there will always be a value in the optional, but if we're writing code
where we don't know if there is a value, we'll need to account for it.
So when we're reading JSON, we can use the NSJSONSerialization object to read the document.
From there we pull out dictionaries, arrays and values.
The only trick is knowing what object you are getting, and then unpacking optionals
when you get to the values.
Thanks for watching!
If you have any more questions let me know in the comments.
New videos out every week, so make sure you subscribe.
Liking the videos let's me know what I'm doing right, so if the tutorial was helpful for
you, let me know!
And with that, I’ll see you in the next tutorial!