Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We'll take a look at the documentation on Json.NET.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
The .NET framework has
a couple of JsonSerializers.
0:00
But we're going to use a third
party serializer called Json.NET.
0:03
It's open sourced and easy to use.
0:07
We'll be using the NuGet package manager
to install it in our application.
0:09
If you wanna learn more about NuGet at
Treehouse check out a link in the notes.
0:14
First we'll right-click on the SoccerStats
project and choose, Manage NuGet Packages.
0:17
Then, under Browse, Json.net is
pretty popular, so it's right here.
0:25
Newtonsoft.Json.
0:30
Here it's chosen the latest stable version
to install, that should be fine for us.
0:33
Click OK and then Finished.
0:37
Let's check the references
to see if it's in there.
0:46
Newtonsoft.Json.
0:49
Before we get started let's check out
the documentation on how to use Json.net.
0:51
We'll search Json.Net.
0:56
There it is.
0:59
Let's see, documentation,
there it, Json.NET Documentation.
1:02
All right, getting started.
1:08
Serializing and deserializing JSON.
1:10
That's what we want.
1:14
Since we're creating objects from JSON,
we'll be deserializing.
1:15
Let's see, the quickest method of
converting between JSON text and
1:21
a .NET object is using the JsonSerializer.
1:24
Sounds good to me.
1:27
We'll skip over JsonConvert and
head right here to JsonSerializer.
1:29
For more control over how
an object is serialized,
1:34
the JsonSerializer can be used directly.
1:37
The JsonSerializer is able to read and
1:40
write JSON text directly to a stream
via JsonTextReader and JsonTextWriter.
1:42
So it sounds like will need a stream.
1:48
And we've got a code sample here.
1:50
Looks like we'll need to instantiate
a JsonSerializer object.
1:54
These properties here look like settings.
1:58
We'll ignore those for now.
2:00
There's a stream here.
2:02
This is using a stream writer
because it's serializing.
2:05
We'll need the opposite of
that which is stream reader.
2:08
Then we'll need a JSON reader
instead of a JSON writer.
2:11
Then it calls the Serialize method.
2:16
I bet there's a Deserialize method too.
2:18
Let's see if we can find
the Deserialize method in the docs.
2:22
See this looks like a link
to the JsonSerializer.
2:26
All right, let's go to the Methods.
2:31
Here it is.
2:36
Looks like there are four overloads.
2:37
These last three methods say that
they deserialize into an instance of
2:41
the specified type.
2:45
This first one attempts
without knowing the type so
2:47
it would have no idea how to do it.
2:50
Let's click on this one that
takes a JSON reader and a type.
2:52
It returns an object, so we'd have to cast
it to our players if we wanted to use it.
2:56
Let's go back.
3:02
This one looks a little different,
with a T in the brackets.
3:06
This is telling us that it's generic.
3:11
The T represents the type parameter and
3:13
you can see here that also
the return type is a T.
3:16
This means that the method will return
the deserialized object as type T.
3:19
In the next video we'll be using this
method to deserialize our players
3:24
JSON file
3:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up