How to extract follower information from Twitch API response using C# and JSON parsing

I’m working on a C# program that needs to fetch the most recent follower data from the Twitch API endpoint. The API returns JSON data with follower details, and I specifically need to extract the username field from this response.

I’ve already figured out how to write text data to a file, but I’m struggling with parsing the JSON response to get the specific field I need. I have Newtonsoft.Json installed in my project, but I can’t seem to get the parsing part working correctly.

Here’s a simple example of what I’m trying to achieve:

using System;
using System.Net.Http;
using System.IO;
using Newtonsoft.Json;

class Program
{
    static async Task Main()
    {
        string apiUrl = "api-endpoint-here";
        HttpClient client = new HttpClient();
        string jsonResponse = await client.GetStringAsync(apiUrl);
        
        // Need help parsing jsonResponse to get username
        // Then save to file
    }
}

Any guidance on how to properly deserialize the JSON and extract the follower name would be really helpful.

quick tip - don’t forget to handle the async properly with await and add error handling around the JsonConvert call in case the api returns malformed json. You can also use JObject.Parse() if u just need one field without creating classes.

To extract follower information from the Twitch API response, you first need to define a model class that aligns with the structure of the JSON returned by the API. For instance, create a FollowerResponse class that contains a list of FollowerData objects. These classes might look like this: public class FollowerResponse { public List<FollowerData> data { get; set; } } and public class FollowerData { public string display_name { get; set; } }. After this, you can deserialize the JSON using JsonConvert.DeserializeObject<FollowerResponse>(jsonResponse) and access the username from the resulting object. Additionally, remember to adhere to Twitch’s authentication requirements, as you’ll need the proper headers in your request to avoid authorization errors.