Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Our network call returns some data in a format known as JSON. Let's take a look at what JSON is and why we're using this particular format.
Teacher's Notes
-
0:00
JSON, also pronounced jason, stands for JavaScript Object Notation, and
-
0:05
is a lightweight data interchange format, that is the preferred means of
-
0:10
transmitting data in web service responses.
-
0:13
Why is it preferred?
-
0:14
Well, JSON is easy for us humans to read and write, and it's easy for machines
-
0:19
to parse and generate, and it's much more lightweight than it's predecessor XML.
-
0:24
This means that less data gets sent over the web, which is both faster and cheaper.
-
0:29
JSON data is made up of primitive data types, like integers, floating point
-
0:35
values, and strings, and can be represented by two types of structures.
-
0:40
First a collection of key-value pairs, and
-
0:43
in different languages this is most commonly realized as a dictionary or
-
0:47
hash table, and second, an ordered list of values.
-
0:51
These can be realized as an array or a list.
-
0:54
Because these two data structures are universal and have support in virtually
-
0:58
all the modern programming languages, it makes a lot of sense to use JSON
-
1:02
as a data format since it is interchangeable with different languages.
-
1:07
If the JSON data is an object, or
-
1:10
a dictionary, it begins with a left curly brace and ends with a right curly brace.
-
1:15
Each key in the key value pair is followed by a colon, and
-
1:19
key value pairs are separated by commas.
-
1:22
If, on the other hand, the object is an ordered collection of values, or an array,
-
1:26
it begins with a left bracket and
-
1:28
ends with a right bracket, with each value being separated by commas.
-
1:33
Now, within a given JSON structure you can encounter nested data, such that
-
1:38
an ordered list can contain a key value collection of information, and vice versa.
-
1:43
We've managed to get some data, but even though the darksky docs said that the data
-
1:48
would come back in the JSON format, this doesn't look like anything, right?
-
1:52
We just have a series of bytes here.
-
1:55
If we go back to the docs, and click on DOCS, and
-
1:59
then go to Response Format, okay, if I go up here, I think.
-
2:05
Yeah, so this is what we want to end up with, right?
-
2:09
So this is the example request, and this is an example response.
-
2:12
But how do we go from the data we have to JSON that looks like this?
-
2:18
Now to do that, we're going to use a class JSON serialization, which has
-
2:23
a class method, JSON object, that takes the data instance and converts it.
-
2:28
Now this conversion isn't always guaranteed to succeed, so
-
2:31
it is also a throwing one.
-
2:33
So we'll say, let json = try!,
-
2:38
forcing it again JSONSerialization.jsonObject
-
2:44
(with: weatherData, options: [ ] ).
-
2:51
Now, as we just saw, JSON can be structured either as a dictionary or
-
2:55
an array at the top level.
-
2:58
To account for this, jsonObject,
-
3:00
this method, returns a value of type any to work with.
-
3:05
So, what we need to do to get our dictionary,
-
3:08
because if we go back to the docs, we can see here that since this response,
-
3:12
the jsonObject begins with a curly brace, and then we have a set of keys and
-
3:16
values, we know that this in Swift, is going to be represented as a dictionary.
-
3:21
So over here what we need to do is, after asking the method to convert our data
-
3:25
object to JSON, we need to cast it to something more appropriate.
-
3:29
So we'll say as, actually you know what, we'll hold off from doing that.
-
3:33
Typically we need to cast the status either a dictionary or array, but
-
3:36
we'll just print that for now.
-
3:37
So print(json), and
-
3:40
you can see, that when you start typing it out, the type is represented as any.
-
3:45
Okay, now if we run this again, the console should pop up, and you will see
-
3:50
now that our data looks exactly like the sample response in the docs.
-
3:55
Where is it?
-
3:55
There we go.
-
3:58
Look at that, keys and values.
-
3:59
We're making some great progress.
-
4:02
You now know how to make a basic networking call, and
-
4:04
get some data from the web.
-
4:06
We could use this instance, this data instance, to populate
-
4:10
an instance of the current weather struct, and display information in our view, but
-
4:14
again there's a bunch of stuff we're doing that is not really the right way.
-
4:18
Why are we doing it this way then?
-
4:20
As always, watch on to find out.
You need to sign up for Treehouse in order to download course files.
Sign up