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