Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Streams and Data Processing Streaming Data on the Net Searching for News Headlines

Kevin Gates
Kevin Gates
15,052 Points

Documentation is out of date now, and no JSON to easily copy and paste.

Carling Kirk : is this something you can update? At least provide the initial JSON structure so we can easily update?

Things have changed on Microsoft's side so it is hard to follow along. Also, there is a 7day subscription free window, so I won't get to do this course ago after a week, unless I pay.

Kevin Gates
Kevin Gates
15,052 Points

I found this, which I think is the more accurate area for at least getting the JSON code: https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/quickstarts/csharp

2 Answers

to get a proper json response code as per your time, i recommend first writing the json code returned by the API using a simple method to output it as a string to a txt file like so;

    public static void WriteJsonToString(string PlayerName, string FilePath)

    {
        var webClient = new WebClient();
        webClient.Headers.Add("Ocp-Apim-Subscription-Key", "your API Key");
        Byte[] playerInfo = 
       webClient.DownloadData(string.Format("https://api.cognitive.microsoft.com/bing/v7.0/news/search?q= 
        {0}&mkt=en-us", PlayerName));
        using (var stream = new MemoryStream(playerInfo))
        using (var writer = new StreamWriter(FilePath))
        using (var reader = new StreamReader(stream))

        {
            string jsonString = reader.ReadToEnd();
            writer.Write(jsonString);
        }
    }

call the method in main using any known soccer player's name like so;

WriteJsonToString("Christiano Ronaldo", "FileName"));

afterwards, go to the output folder and get the txt file created under the fileName that you specified, open the file with a notepad and copy that json code, then use an online json formatter or validator, i personally recommend https://jsonformatter.curiousconcept.com/

simply paste in the json code you copied from the txt file that your program created and it will format it into a proper Json format, then you can now use this json code with the visual studio's paste special - paste json as classes

On a further Video, Carling Kirk shows you how VALIDATE and UPDATE any Json Code so that it can be Special Pasted / Paste JSON as classes into Visual Studio.

Copy the Json code from https://docs.microsoft.com/en-us/azure/cognitive-services/bing-news-search/concepts/search-for-news#get-general-news

Type it into https://jsonlint.com/

Remove any unwanted Json Code.

Now it is ready to paste into Visual Studio.