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

Kevin Gates
Kevin Gates
14,707 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
14,707 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

1 Answer

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