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 clean up the Player class and use attributes on our class properties.
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
Unfortunately our JSON file
has property names that
0:00
aren't the proper naming convention in C#.
0:03
We need to clean it up a bit.
0:06
Our player class also has a bunch
of fields we really don't need.
0:08
We can ignore a lot of these fields.
0:12
Actually, I don't even know
what half of them mean.
0:14
We can just delete them and
they won't be de-serialized.
0:18
We need to figure out which
fields we need though.
0:21
I have an idea about what
we can use this data for.
0:24
We can see who the top ten scoring
players in the MLS are this season.
0:28
For that we'll only need
fields like name and points.
0:32
I'll delete everything
here up to first name.
0:37
Then I'll keep ID, then delete
everything except for points per game.
0:44
And then we'll keep second
name which is the last name.
0:58
Then we'll also keep the team name.
1:03
Everything else can go.
1:08
Now we can rename
the properties we need but
1:12
some of these will deserialize
just fine if we change the case.
1:14
But when we start taking out
things like underscores, we need
1:18
to tell the serializer what to look for
when trying to deserialize this property.
1:22
This is a common task in serializing,
so let's go to Google and
1:27
see if anyone else has
come across this scenario.
1:30
Let's see, json serialize
1:34
property to different name.
1:41
Let's check this first link
here from Stack Overflow.
1:48
What would we do without stack overflow?
1:52
Let's see.
1:56
Looks like they're trying to
do the same thing we are.
2:01
Let's check out the answer.
2:03
You could decorate the property you wish
controlling its name with the JSONProperty
2:06
attribute which allows you
to specify a different name.
2:10
Great.
Let's try it out.
2:13
We can copy this attribute here.
2:15
And above our first name property, we
can paste what we found from the answer.
2:19
Looks like we need to add
the JSON.NET namespace to our class.
2:24
A quick way we can do this is
to right-click on JSONProperty,
2:28
since it's got the red squiggly line,
and choose Quick Actions.
2:32
The first one here wants to
add the using directive and
2:37
now a red squiggly line is gone.
2:43
Then we can change this
property name to property name.
2:46
And then we'll change this
to FirstName in proper case.
2:52
Now let's see if it will
serialize the first name.
2:59
We can go back to our main method and
print out the first name to the console.
3:02
FirstName.
3:08
Let's see if adding the attribute worked.
3:09
Add a break point and F5.
3:13
All right,
these look like first names to me.
3:19
You might not have seen
an attribute before.
3:27
In C#, attributes are a way to
decorate classes, properties, and
3:30
methods with some additional
information about them.
3:34
It's like metadata.
3:37
They don't really do anything, but
3:38
they can be used to determine if
something should be done with them.
3:40
You can even create your own attributes,
3:43
[LAUGH] but that's definitely
a subject for another course.
3:45
Let's go and do the rest.
3:49
Why don't you pause the video and
3:51
get some practice adding these
attributes to our class?
3:52
Now I'll paste in my code.
3:57
Now let's run it again and
take a look at our properties.
4:06
Breakpoint is still there, so F5.
4:10
And hover over players.
4:15
And looks like our properties
are serializing just fine.
4:19
But look at points per game,
it's a string.
4:24
That should definitely be a number.
4:27
We can just change the type in
the player class to a double and
4:30
JSON.net should do the parsing for us.
4:35
Now let's run it again and
4:41
see if our points per game is
being serialized correctly.
4:42
That's a 0, that's a 2, looks good.
4:54
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