0

I am trying to consume my api from another api, so I am using an HttpClient. My code Looks as follows:

 public async Task<List<TeamName>> GetIpAddressDetails()
        {
            HttpClient client = new HttpClient();
            try
            {
                HttpResponseMessage httpResponse = await client.GetAsync(adminURL);
                var content = await httpResponse.Content.ReadAsStringAsync();
                var resp = new StringContent(JsonConvert.SerializeObject(content), System.Text.Encoding.UTF8, "application/json");
                return content

What I am trying to achieve is map the response which gets returned as JSON from my other API into a list of TeamName, the TeamName POCO matches exactly to what is in the API. It looks like this:

public class TeamName
{
    [JsonProperty("guid")]
    public Guid Guid { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("priorityNumber")]
    public int PriorityNumber { get; set; }
}

I am confused on how to return the content as a list of TeamName

Mohammed
  • 66
  • 13
  • Does this answer your question? [How to Convert JSON object to Custom C# object?](https://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – DubDub Apr 13 '22 at 09:11
  • On the client `var teamNames = JsonSerializer.Deserialize>(content);` – MindSwipe Apr 13 '22 at 09:11

1 Answers1

1

You can use DeserializeObject method from Newtonsoft.Json.JsonConvert class like this:

var response = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<TeamName>>(response);
Sina Riani
  • 325
  • 4
  • 17