1 00:00:00,210 --> 00:00:03,360 We can expand the types of data in our soccer stats application 2 00:00:03,360 --> 00:00:06,620 by adding details for the players on each team. 3 00:00:06,620 --> 00:00:08,060 I've done a little searching, and 4 00:00:08,060 --> 00:00:12,420 I found a GitHub repository that shares player information and statistics. 5 00:00:12,420 --> 00:00:14,030 It's formatted in JSON, so 6 00:00:14,030 --> 00:00:17,270 we can serialize it to objects in our application. 7 00:00:17,270 --> 00:00:20,040 First, we need to create a class that will represent a player. 8 00:00:21,060 --> 00:00:23,290 Let's take a look at the JSON file. 9 00:00:23,290 --> 00:00:27,340 The first thing I notice is this open bracket at the beginning. 10 00:00:27,340 --> 00:00:29,702 This is telling me that it's an array. 11 00:00:29,702 --> 00:00:33,580 And then this open curly brace here is the start of an object. 12 00:00:34,690 --> 00:00:37,440 Let's scroll down a bit until we see an ending curly brace. 13 00:00:39,010 --> 00:00:39,600 Here it is. 14 00:00:41,730 --> 00:00:44,380 So that's the end of our first object. 15 00:00:44,380 --> 00:00:47,570 Each of these lines inside the curly braces are properties of 16 00:00:47,570 --> 00:00:49,290 the player object. 17 00:00:49,290 --> 00:00:53,970 And we have a property name followed by a colon and then the property value. 18 00:00:55,320 --> 00:00:55,830 Each name and 19 00:00:55,830 --> 00:01:00,680 value is followed by a comma to separate them until we reach the end of the object. 20 00:01:01,800 --> 00:01:06,170 And then a comma follows each object in the file. 21 00:01:06,170 --> 00:01:08,355 Let's save this file to our project directory. 22 00:01:08,355 --> 00:01:12,508 Right-click on the page and Save as. 23 00:01:12,508 --> 00:01:19,080 I'll need to change this to All Files and take out this .txt. 24 00:01:19,080 --> 00:01:21,950 You can find this file in the project files link 25 00:01:21,950 --> 00:01:23,870 in the download section below the video. 26 00:01:25,400 --> 00:01:27,410 Now back to our project. 27 00:01:27,410 --> 00:01:32,488 We can add the file with a right-click, Add > Existing item. 28 00:01:35,939 --> 00:01:42,180 Find our, change that All Files, and players.json. 29 00:01:42,180 --> 00:01:45,300 Before we open it up, we'll need to make sure it gets copied to the output 30 00:01:45,300 --> 00:01:48,330 directory in the properties like we did with the CSV file. 31 00:01:51,100 --> 00:01:53,346 Right-click and Properties. 32 00:01:53,346 --> 00:01:56,195 Down here, change to Copy if newer. 33 00:01:56,195 --> 00:01:57,890 Looks good. 34 00:01:57,890 --> 00:02:01,340 There's a neat little feature in Visual Studio that we're going to use to create 35 00:02:01,340 --> 00:02:03,560 a class from our JSON file. 36 00:02:03,560 --> 00:02:05,760 First, we need a new class. 37 00:02:05,760 --> 00:02:09,090 Add > Class. 38 00:02:09,090 --> 00:02:10,351 And we'll name it player. 39 00:02:13,500 --> 00:02:18,495 We can go back to the JSON file and select all with Ctrl+A. 40 00:02:19,660 --> 00:02:21,190 And copy with Ctrl+C. 41 00:02:22,650 --> 00:02:25,350 Now go back to our Player.cs file. 42 00:02:25,350 --> 00:02:33,080 I'll highlight here and say Edit > Paste Special > Paste JSON as Classes. 43 00:02:33,080 --> 00:02:36,670 This Visual Studio feature parses the information from the file and 44 00:02:36,670 --> 00:02:39,490 attempts to figure out the data types for us. 45 00:02:39,490 --> 00:02:43,060 And now we don't have to type out all the names into our class. 46 00:02:43,060 --> 00:02:44,420 How cool. 47 00:02:44,420 --> 00:02:47,420 However, you can see that the properties are lowercase, 48 00:02:47,420 --> 00:02:50,570 which isn't the convention for public properties of a class. 49 00:02:50,570 --> 00:02:51,670 We'll need to fix that later. 50 00:02:53,030 --> 00:02:56,210 Visual Studio has given us two classes here. 51 00:02:56,210 --> 00:03:00,145 One is called Rootobject, and the other is called Class1. 52 00:03:00,145 --> 00:03:03,767 In the JSON file, the player objects were unnamed, so 53 00:03:03,767 --> 00:03:06,383 it just assigned one for us, Class1. 54 00:03:06,383 --> 00:03:07,852 We can change that to Player. 55 00:03:11,941 --> 00:03:15,750 And the Rootobject sort of represents the file itself. 56 00:03:15,750 --> 00:03:17,050 We can name it whatever we want, 57 00:03:17,050 --> 00:03:21,430 but I'll just change that lowercase o to a capital O. 58 00:03:22,510 --> 00:03:26,630 Inside the RootObject holds our array of Player objects. 59 00:03:26,630 --> 00:03:31,398 We can rename that to Players and make sure of the type is a Player array. 60 00:03:35,212 --> 00:03:39,170 Next, we'll be deserializing our JSON file to Player objects.