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 use Json.NET to deserialize the news headlines from the Bing News Search API.
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
In our GetNewsForPlayer method,
0:00
lets add some deserialization
logic to return objects.
0:02
Let's hop over to our NewsSearch class
first and make it a little prettier.
0:06
Let's rename the Value
class here to NewsResult.
0:10
And we'll need to change it up here.
0:18
I want to change this array to a list.
0:20
Json.NET will be able to
serialize it just fine.
0:24
And we'll rename Value to NewsResults.
0:30
And we'll need our JsonProperty again,
JsonProperty and
0:33
Ctrl+period, Enter.
0:38
Then, PropertyName = "value" and
0:42
I'll go ahead and copy this.
0:47
So down here, our name was our headline.
0:53
Let's call it Headline,
0:57
then the PropertyName was name.
1:01
And then in the description,
we'll change that to Summary.
1:06
Copy this attribute one time here.
1:14
And we'll change datePublished and
just capitalize the D.
1:17
All right, save with Ctrl+S.
1:26
Let's hop back over to Program.
1:28
We'll go ahead and change the return
1:31
type here to a List of NewsResults.
1:35
And go ahead and create our list.
1:40
var results = new List of NewsResult And
1:44
we'll go ahead and return results.
1:51
Now we can add our deserialization logic.
1:55
We'll need a JSON serializer.
1:57
var serializer = new JsonSerializer.
2:01
And we've already got a stream.
2:08
So down here, using var jsonReader
2:11
= new JsonTextReader.
2:18
And we'll pass it the stream reader.
2:23
Now, inside here, we'll delete that, and
2:26
we'll assign results equals
serializer.Deserialize.
2:31
And the type will be NewsSearch,
2:37
because the whole JSON response
is a NewsSearch object.
2:40
And we'll pass it the jsonReader.
2:46
But then inside the NewsSearch,
we've got our NewsResults list.
2:49
Looks like we're ready to search for
each player in our list.
2:56
Back up in main, we'll need to uncomment
our code where we deserialize our players.
3:00
I can hold down the Ctrl key and
hit K, U to uncomment everything.
3:07
We can get rid of this line where we
just print the results to the screen.
3:14
And we can reuse this foreach loop.
3:19
foreach player in topTenPlayers.
3:21
We'll take this out.
3:24
Then we'll make the call
to GetNewsForPlayer.
3:27
List of NewsResults newsResults
equals GetNewsForPlayer.
3:30
Then we'll pass it the player name, hm,
3:38
our player object has a property for
FirstName and LastName, so
3:41
we'll need to stick those together
in order to search on the full name.
3:46
We can use String.Format
with two index placeholders.
3:52
String.Format.
3:55
And inside the string,
we'll need a curly brace with a 0 and
4:00
a space and a curly brace with a 1.
4:06
Then we'll pass it
the player.FirstName and
4:10
the player.LastName.
4:16
All right, semicolon.
4:19
Now we can iterate through the results and
print something to the console.
4:22
foreach var result in newsResults.
4:26
Then in here, we'll write to the console.
4:35
WriteLine.
4:38
And let's use String.Format again
to make it a little easier to read.
4:42
String.Format.
4:45
Inside here, we'll print the date, 0,
4:51
then we'll print the headline,
give that a 1.
4:57
And then a summary.
5:04
Give that a 2.
5:08
Now we need to pass the three
parameters to the String.Format method.
5:10
So that's result.DatePublished,
5:15
then result.Headline, then result.Summary.
5:20
And then we made a semicolon.
5:27
You know, we could also stick in a new
line to separate each news result.
5:29
Do you remember those escape characters?
5:34
Backslash r, backslash n.
5:38
Then at the end, why don't we go ahead and
throw in a Console.ReadKey so
5:41
that we can look at each
player's results at a time.
5:47
All right, everything looks good.
5:52
Let's go ahead and press F5 to run.
5:53
Everything looks good.
5:58
So notice the date here.
6:04
We can actually use
a different formatter for
6:06
the date value in the String.Format
method so that it formats it different.
6:09
Let's take a look at some
documentation on formatting dates.
6:13
So using these specifiers in our
placeholders will let us control how
6:20
the dates get printed.
6:24
Let's try one of these out.
6:26
Notice that they're showing
different outputs here to the right.
6:28
The outputs can vary based
on a culture info setting.
6:31
Let's use this one, the letter f.
6:36
So right here, for
our date, we'll do a :f.
6:41
And let's see what that looks like.
6:48
F5.
6:49
All right,
now we've got a much longer date.
6:53
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