title: Deserialize JSON to Array or List with HTTPClient .ReadAsAsync using .NET 4.0 Task pattern
body: <p>I'm trying to deserialize the JSON returned from <code>http://api.usa.gov/jobs/search.json?query=nursing+jobs</code> using the .NET 4.0 Task pattern.  It returns this JSON ('Load JSON data' @ <code>http://jsonviewer.stack.hu/</code>).</p>
  
  <pre><code>[
    {
      "id": "usajobs:353400300",
      "position_title": "Nurse",
      "organization_name": "Indian Health Service",
      "rate_interval_code": "PA",
      "minimum": 42492,
      "maximum": 61171,
      "start_date": "2013-10-01",
      "end_date": "2014-09-30",
      "locations": [
        "Gallup, NM"
      ],
      "url": "https://www.usajobs.gov/GetJob/ViewDetails/353400300"
    },
    {
      "id": "usajobs:359509200",
      "position_title": "Nurse",
      "organization_name": "Indian Health Service",
      "rate_interval_code": "PA",
      "minimum": 42913,
      "maximum": 61775,
      "start_date": "2014-01-16",
      "end_date": "2014-12-31",
      "locations": [
        "Gallup, NM"
      ],
      "url": "https://www.usajobs.gov/GetJob/ViewDetails/359509200"
    },
    ...
  ]
  </code></pre>
  
  <p>Index Action:</p>
  
  <pre><code>  public class HomeController : Controller
    {
      public ActionResult Index()
      {
        Jobs model = null;
        var client = new HttpClient();
        var task = client.GetAsync("http://api.usa.gov/jobs/search.json?query=nursing+jobs")
          .ContinueWith((taskwithresponse) =&gt;
          {
            var response = taskwithresponse.Result;
            var jsonTask = response.Content.ReadAsAsync&lt;Jobs&gt;();
            jsonTask.Wait();
            model = jsonTask.Result;
          });
        task.Wait();
        ...
       }
  </code></pre>
  
  <p>Jobs and Job class:</p>
  
  <pre><code>  [JsonArray]
    public class Jobs { public List&lt;Job&gt; JSON; }
  
    public class Job
    {
      [JsonProperty("organization_name")]
      public string Organization { get; set; }
      [JsonProperty("position_title")]
      public string Title { get; set; }
    }
  </code></pre>
  
  <p>When I set a breakpoint on <code>jsonTask.Wait();</code> and examine <code>jsonTask</code> the status is
  Faulted.  The InnerException is "Type ProjectName.Jobs is not a collection."</p>
  
  <p>I started with the Jobs type without the JsonArray attribute and Jobs as an array (Job[]) and got this error.</p>
  
  <pre><code>  public class Jobs { public Job[] JSON; }
  
      +       InnerException  {"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ProjectName.Models.Jobs' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\n
      To fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface
   (e.g. ICollection, IList) like List&lt;T&gt; that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\n
  Path '', line 1, position 1."}  System.Exception {Newtonsoft.Json.JsonSerializationException}
  </code></pre>
  
  <p>How would I process this site's JSON with the .NET 4.0 Task pattern?  I would like to get this working before moving onto the <code>await async</code> pattern in .NET 4.5.</p>
  
  <p><strong>ANSWER UPDATE:</strong></p>
  
  <p>Here's an example using the .NET 4.5 async await pattern with brumScouse's answer.</p>
  
  <pre><code> public async Task&lt;ActionResult&gt;Index()
   {
      List&lt;Job&gt; model = null;
      var client = newHttpClient();
  
      // .NET 4.5 async await pattern
      var task = await client.GetAsync(http://api.usa.gov/jobs/search.json?query=nursing+jobs);
      var jsonString = await task.Content.ReadAsStringAsync();
      model = JsonConvert.DeserializeObject&lt;List&lt;Job&gt;&gt;(jsonString);
      returnView(model);
   }
  </code></pre>
  
  <p>You will need to bring in the <code>System.Threading.Tasks</code> namespace.<br>
  <em>Note:</em> there is no <code>.ReadAsString</code> method available on <code>.Content</code> which is why I used the <code>.ReadAsStringAsync</code> method.</p>