1 00:00:00,460 --> 00:00:03,720 Unfortunately our JSON file has property names that 2 00:00:03,720 --> 00:00:06,550 aren't the proper naming convention in C#. 3 00:00:06,550 --> 00:00:08,680 We need to clean it up a bit. 4 00:00:08,680 --> 00:00:12,650 Our player class also has a bunch of fields we really don't need. 5 00:00:12,650 --> 00:00:14,980 We can ignore a lot of these fields. 6 00:00:14,980 --> 00:00:18,090 Actually, I don't even know what half of them mean. 7 00:00:18,090 --> 00:00:21,580 We can just delete them and they won't be de-serialized. 8 00:00:21,580 --> 00:00:24,790 We need to figure out which fields we need though. 9 00:00:24,790 --> 00:00:28,400 I have an idea about what we can use this data for. 10 00:00:28,400 --> 00:00:32,960 We can see who the top ten scoring players in the MLS are this season. 11 00:00:32,960 --> 00:00:37,090 For that we'll only need fields like name and points. 12 00:00:37,090 --> 00:00:44,210 I'll delete everything here up to first name. 13 00:00:44,210 --> 00:00:52,465 Then I'll keep ID, then delete everything except for points per game. 14 00:00:58,121 --> 00:01:01,360 And then we'll keep second name which is the last name. 15 00:01:03,720 --> 00:01:05,600 Then we'll also keep the team name. 16 00:01:08,380 --> 00:01:09,480 Everything else can go. 17 00:01:12,140 --> 00:01:14,940 Now we can rename the properties we need but 18 00:01:14,940 --> 00:01:18,710 some of these will deserialize just fine if we change the case. 19 00:01:18,710 --> 00:01:22,740 But when we start taking out things like underscores, we need 20 00:01:22,740 --> 00:01:27,490 to tell the serializer what to look for when trying to deserialize this property. 21 00:01:27,490 --> 00:01:30,850 This is a common task in serializing, so let's go to Google and 22 00:01:30,850 --> 00:01:33,050 see if anyone else has come across this scenario. 23 00:01:34,910 --> 00:01:39,150 Let's see, json serialize 24 00:01:41,970 --> 00:01:46,160 property to different name. 25 00:01:48,520 --> 00:01:51,350 Let's check this first link here from Stack Overflow. 26 00:01:52,560 --> 00:01:54,470 What would we do without stack overflow? 27 00:01:56,600 --> 00:01:57,100 Let's see. 28 00:02:01,000 --> 00:02:03,750 Looks like they're trying to do the same thing we are. 29 00:02:03,750 --> 00:02:04,980 Let's check out the answer. 30 00:02:06,530 --> 00:02:10,440 You could decorate the property you wish controlling its name with the JSONProperty 31 00:02:10,440 --> 00:02:13,920 attribute which allows you to specify a different name. 32 00:02:13,920 --> 00:02:15,008 Great. Let's try it out. 33 00:02:15,008 --> 00:02:19,672 We can copy this attribute here. 34 00:02:19,672 --> 00:02:23,280 And above our first name property, we can paste what we found from the answer. 35 00:02:24,530 --> 00:02:28,930 Looks like we need to add the JSON.NET namespace to our class. 36 00:02:28,930 --> 00:02:32,563 A quick way we can do this is to right-click on JSONProperty, 37 00:02:32,563 --> 00:02:36,090 since it's got the red squiggly line, and choose Quick Actions. 38 00:02:37,780 --> 00:02:43,780 The first one here wants to add the using directive and 39 00:02:43,780 --> 00:02:45,190 now a red squiggly line is gone. 40 00:02:46,390 --> 00:02:49,890 Then we can change this property name to property name. 41 00:02:52,960 --> 00:02:59,635 And then we'll change this to FirstName in proper case. 42 00:02:59,635 --> 00:03:02,211 Now let's see if it will serialize the first name. 43 00:03:02,211 --> 00:03:08,340 We can go back to our main method and print out the first name to the console. 44 00:03:08,340 --> 00:03:09,765 FirstName. 45 00:03:09,765 --> 00:03:13,336 Let's see if adding the attribute worked. 46 00:03:13,336 --> 00:03:19,190 Add a break point and F5. 47 00:03:19,190 --> 00:03:21,200 All right, these look like first names to me. 48 00:03:27,280 --> 00:03:30,272 You might not have seen an attribute before. 49 00:03:30,272 --> 00:03:34,020 In C#, attributes are a way to decorate classes, properties, and 50 00:03:34,020 --> 00:03:37,250 methods with some additional information about them. 51 00:03:37,250 --> 00:03:38,760 It's like metadata. 52 00:03:38,760 --> 00:03:40,220 They don't really do anything, but 53 00:03:40,220 --> 00:03:43,950 they can be used to determine if something should be done with them. 54 00:03:43,950 --> 00:03:45,620 You can even create your own attributes, 55 00:03:45,620 --> 00:03:49,460 [LAUGH] but that's definitely a subject for another course. 56 00:03:49,460 --> 00:03:51,320 Let's go and do the rest. 57 00:03:51,320 --> 00:03:52,720 Why don't you pause the video and 58 00:03:52,720 --> 00:03:55,130 get some practice adding these attributes to our class? 59 00:03:57,549 --> 00:03:59,259 Now I'll paste in my code. 60 00:04:06,884 --> 00:04:10,634 Now let's run it again and take a look at our properties. 61 00:04:10,634 --> 00:04:12,637 Breakpoint is still there, so F5. 62 00:04:15,933 --> 00:04:17,859 And hover over players. 63 00:04:19,380 --> 00:04:24,104 And looks like our properties are serializing just fine. 64 00:04:24,104 --> 00:04:27,349 But look at points per game, it's a string. 65 00:04:27,349 --> 00:04:29,110 That should definitely be a number. 66 00:04:30,170 --> 00:04:35,106 We can just change the type in the player class to a double and 67 00:04:35,106 --> 00:04:38,473 JSON.net should do the parsing for us. 68 00:04:41,430 --> 00:04:42,898 Now let's run it again and 69 00:04:42,898 --> 00:04:46,178 see if our points per game is being serialized correctly. 70 00:04:54,553 --> 00:04:58,950 That's a 0, that's a 2, looks good.