Deserialize the News Result

In our NewsSearch class, do the following change:

  1. Change class named "Value" to "NewsResult"
  2. Change Value[] value to List<NewsResult> NewsResults
  3. Put this code above no.2 : [JsonProperty(PropertyName = "value")
  4. Change name in NewsResult class to Headline and put JsonProperty
  5. Change description to Summary and put JsonProperty
  6. Back to Main class, change type of GetNewsFromPlayer from string to List<NewsResult>

Add following code.

// for initiating a variable
var results = new List<NewsResult>();

// for using serializer in Json
var serializer = new JsonSerializer();

To deserialize the data stream, we used json's deserializer like we had before.

using (var stream = new MemoryStream(searchResult))
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
    results = serializer.Deserialize<NewsSearch>(jsonReader).NewsResults;
}
return results;

We have done with the function. Now, we look at the Main method.

We need to store the result from GetNewsFromPlayer into newsResult list. Then, we display our data by its datePublished, headline and summary.

foreach (var player in topTenPlayers)
{
    List<NewsResult> newsResults = GetNewsFromPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));
    foreach (var news in newsResults)
    {
        Console.WriteLine(string.Format("Date: {0} \nHeadline: {1} \nSummary: {2} \r\n", news.datePublished, news.Headline, news.Summary));
        Console.ReadKey();
    }
}

results matching ""

    No results matching ""