Home > Back-end >  .NET 6 JsonSerializer.Deserialize returns defined array with empty objects
.NET 6 JsonSerializer.Deserialize returns defined array with empty objects

Time:12-15

Currently I am trying to deserialize a json string from a http response. I have verified the string returned is valid JSON. When the deserialize function completes I have an array with undefined objects.

public async Task<IEnumerable<MemberRoster>> GetAsync(string groupNumber)
    {
        var httpClient = _httpClientFactory.CreateClient();
        httpClient.DefaultRequestHeaders.Accept.Clear();
        httpClient.DefaultRequestHeaders.Add("client_id", Configuration["Microservices:RequestHeaders:client_id"]);
        httpClient.DefaultRequestHeaders.Add("client_secret", Configuration["Microservices:RequestHeaders:client_secret"]);

        var queryParams = new Dictionary<string, string>()
        {
            ["groupNumber"] = groupNumber
        };

        var memberUri = QueryHelpers.AddQueryString(Configuration["Microservices:Member"], queryParams);
        var response = await httpClient.GetAsync(memberUri).Result.Content.ReadAsStringAsync();
        var data = JsonSerializer.Deserialize<IEnumerable<MemberRoster>>(response);

        return data;
    }

public class MemberRoster
{
    [JsonPropertyName("health_id")]
    string HealthId { get; set; }
};

enter image description here

JSON in response

enter image description here

enter image description here

CodePudding user response:

I need to add the public accessor to my properties.

  • Related