This course will be retired on June 1, 2025.
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 create a class to hold our soccer player data.
This video doesn't have any notes.
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
We can expand the types of data
in our soccer stats application
0:00
by adding details for
the players on each team.
0:03
I've done a little searching, and
0:06
I found a GitHub repository that shares
player information and statistics.
0:08
It's formatted in JSON, so
0:12
we can serialize it to
objects in our application.
0:14
First, we need to create a class
that will represent a player.
0:17
Let's take a look at the JSON file.
0:21
The first thing I notice is this
open bracket at the beginning.
0:23
This is telling me that it's an array.
0:27
And then this open curly brace
here is the start of an object.
0:29
Let's scroll down a bit until
we see an ending curly brace.
0:34
Here it is.
0:39
So that's the end of our first object.
0:41
Each of these lines inside
the curly braces are properties of
0:44
the player object.
0:47
And we have a property name followed
by a colon and then the property value.
0:49
Each name and
0:55
value is followed by a comma to separate
them until we reach the end of the object.
0:55
And then a comma follows
each object in the file.
1:01
Let's save this file to
our project directory.
1:06
Right-click on the page and Save as.
1:08
I'll need to change this to All Files and
take out this .txt.
1:12
You can find this file in
the project files link
1:19
in the download section below the video.
1:21
Now back to our project.
1:25
We can add the file with a right-click,
Add > Existing item.
1:27
Find our, change that All Files,
and players.json.
1:35
Before we open it up, we'll need to
make sure it gets copied to the output
1:42
directory in the properties
like we did with the CSV file.
1:45
Right-click and Properties.
1:51
Down here, change to Copy if newer.
1:53
Looks good.
1:56
There's a neat little feature in Visual
Studio that we're going to use to create
1:57
a class from our JSON file.
2:01
First, we need a new class.
2:03
Add > Class.
2:05
And we'll name it player.
2:09
We can go back to the JSON file and
select all with Ctrl+A.
2:13
And copy with Ctrl+C.
2:19
Now go back to our Player.cs file.
2:22
I'll highlight here and say Edit >
Paste Special > Paste JSON as Classes.
2:25
This Visual Studio feature parses
the information from the file and
2:33
attempts to figure out the data types for
us.
2:36
And now we don't have to type out
all the names into our class.
2:39
How cool.
2:43
However, you can see that
the properties are lowercase,
2:44
which isn't the convention for
public properties of a class.
2:47
We'll need to fix that later.
2:50
Visual Studio has given
us two classes here.
2:53
One is called Rootobject, and
the other is called Class1.
2:56
In the JSON file,
the player objects were unnamed, so
3:00
it just assigned one for us, Class1.
3:03
We can change that to Player.
3:06
And the Rootobject sort of
represents the file itself.
3:11
We can name it whatever we want,
3:15
but I'll just change that
lowercase o to a capital O.
3:17
Inside the RootObject holds
our array of Player objects.
3:22
We can rename that to Players and
make sure of the type is a Player array.
3:26
Next, we'll be deserializing our
JSON file to Player objects.
3:35
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