Call WebAPI then parse part of json result this is a basic web api call using HttpClient and parse some part of the result, this was working as expected, but the part inside the if (response.IsSuccessStatusCode) doesnt seems right. It feels like it can be simplified, but I don't know how.

static async Task&lt;List&lt;Hero&gt;&gt; GetHero(string url, string urlParameters)
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    using (var response = client.GetAsync(urlParameters).Result)
    {
        if (response.IsSuccessStatusCode)
        {
            var heroes = new List&lt;Hero&gt;();
            var jsonResult = await response.Content.ReadAsStringAsync();
            var deserializedjsonResult = JObject.Parse(jsonResult);
            var resultHeroes = deserializedjsonResult["result"]["heroes"].ToList();

            resultHeroes.ForEach(x =&gt; heroes.Add(x.ToObject&lt;Hero&gt;()));

            return heroes;
        }
    }

    return null;
}


the api that I was calling will return something in this format

{
  "result": {
    "heroes": [
      {
        ...
      },
      {
        ...
      }
    ],
    "villains": [
      {
        ...
      },
      {
        ...
      }
    ],
    "extras": [
      {
        ...
      },
      {
        ...
      }
    ],
  }
}
HttpClient client = new HttpClient();



This needs to be disposed.




using (var response = client.GetAsync(urlParameters).Result)



Why don't you await this like you do with other awaitable APIs?




static async Task&lt;List&lt;Hero&gt;&gt; GetHero(string url, string urlParameters)
{
    ..
    return null;
}



Methods returning collections should return an empty collection instead of null this means you should

return Task.FromResult(new List&lt;Hero&gt;());




As far as the if block is concerned you don't need the heros variable. With LINQ can create the list inplace:

if (response.IsSuccessStatusCode)
{
    var jsonResult = await response.Content.ReadAsStringAsync();
    var deserializedjsonResult = JObject.Parse(jsonResult);
    var resultHeroes = deserializedjsonResult["result"]["heroes"];
    return resultHeroes.Select(x =&gt; x.ToObject&lt;Hero&gt;()).ToList();
}