Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Deserializing with Json.NET4:44 with Carling Kirk
We'll use the JsonSerializer to Deserialize the soccer player JSON file.
Let's get back to coding. 0:00 Though, just so you know, 0:02 being a programmer means that a lot of the time you'll be reading the documentation. 0:04 Here, in our program class we'lll create a method that will return a list of players. 0:09 Let's call it the serialized players. 0:13 It should return a list of players. 0:19 Public static List of Player, 0:21 And I got a double c here and the DeserializePlayers. 0:29 We'll go ahead and add our, var players 0:39 equals new List of Player, and return it. 0:44 We'll need our JSON serializer object. 0:55 But first we should add the JSON dot net namespace to our using directives list. 0:58 Using Newtonsoft dot JSON. 1:03 Okay now. 1:11 var serializer. 1:13 Equals new JsonSerializer. 1:18 Let's check out what's on the serializer object. 1:24 Serializer dot, these properties look like some kind of settings. 1:27 And here's our Deserialize method we can use. 1:35 Let's go ahead and make a stream reader for our file. 1:40 Don't forget the using statement. 1:44 Bar reader equals new stream 1:46 reader and only no filename. 1:51 We'll need to pass in a filename like we did in an earlier video. 1:58 Put that up here string fileName. 2:02 And come back down to our using and make a code bracket. 2:06 You can stick this serializer.Deserialize back up in there. 2:10 Now we need that JSON reader. 2:15 When we need multiple using statements we could add them both before 2:17 the curly braces like we saw in the JSON.net code sample. 2:21 So right here, using var jsonReader 2:25 equals new JSONTextReader and 2:30 then we'll pass it our stream reader object. 2:35 So, down here will go back to serialize and type in angle bracket. 2:42 So, as soon as I type the angle bracket, Visual Studio popped up the documentation 2:47 on the method, so convenient T, the type of object to deserialize. 2:52 We eventually want a list of players, right? 2:58 Well, since our file contains a json array, 3:00 json.net should be able to deserialize that array into a list. 3:03 Then we can just assign it to our players variable 3:09 <List<Player And pass it the JSON reader. 3:13 Then we can just assign the results of the deserialization to players. 3:22 Now it's time to call our method in main. 3:28 I'm gonna copy this. 3:30 We'l need to pass our method the filename here so 3:35 let's reuse this file name property here paste 3:41 that there copy this line and change this to players dot json. 3:46 Then we can call deserialize players with the file name. 3:56 And create a variable to store it in, var players = DeserializePlayers. 4:02 Now to see if the deserialization was successful, 4:08 let's print out the names of the soccer players to the console. 4:10 We use a for each loop. 4:14 For each (var player in players) then 4:15 we'll use Console.WriteLine, 4:20 player will just use the last name which is second name. 4:24 All right, let's set a breakpoint and run it with f5. 4:32 And there's our players. 4:41
You need to sign up for Treehouse in order to download course files.Sign up