title: c# Deserialize json into List
body: <p>I have this code that retrieves Json string from API link. The Json is deserialized and returned to a textbox. This works perfectly as long as there is only 1 Json value, being returned more than 1 value it crashes with this error:</p>
  
  <blockquote>
    <p>Additional information: Cannot deserialize the current JSON array
    (e.g. [1,2,3]) into type 'GW2_tradingPost.RootObject' because the type
    requires a JSON object (e.g. {"name":"value"}) to deserialize
    correctly.</p>
    
    <p>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 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.</p>
  </blockquote>
  
  <p>Doing my research this happens because there is nowhere to deposit the Json since its not an List.</p>
  
  <p>I've have tried this code and various similar ones.</p>
  
  <pre><code>List&lt;RootObject&gt; list = JsonConvert.DeserializeObject&lt;List&lt;RootObject&gt;&gt;(jsonReader.ToString());
  return list;
  </code></pre>
  
  <p>This will return with error:</p>
  
  <blockquote>
    <p>Error 1   Cannot implicitly convert type
    'System.Collections.Generic.List' to
    'GW2_tradingPost.RootObject'  e:\mega\gw2_tradingpost\gw2_tradingpost\api_request.cs  34</p>
  </blockquote>
  
  <p>Which i dont fully understand what it means.</p>
  
  <p>Here is my full code.</p>
  
  <p>api_Request.cs</p>
  
  <pre><code>public class api_Request
      {
          public RootObject GetApi(string url)
          {
  
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  
              try
              {
                  WebResponse response = request.GetResponse();
                  using (Stream responseStream = response.GetResponseStream())
                  {
                   StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                  var jsonReader = new JsonTextReader(reader);
                  var serializer = new JsonSerializer();
                  return serializer.Deserialize&lt;RootObject&gt;(jsonReader);
              }
              }
              catch (WebException ex)
              {
                  WebResponse errorResponse = ex.Response;
                  using (Stream responseStream = errorResponse.GetResponseStream())
                  {
                      StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                      String errorText = reader.ReadToEnd();
                      // log errorText
                  }
                  throw;
              }
          }
  
      }
  
  
      public class Buy
      {
          public int listings { get; set; }
          public int unit_price { get; set; }
          public int quantity { get; set; }
      }
  
      public class Sell
      {
          public int listings { get; set; }
          public int unit_price { get; set; }
          public int quantity { get; set; }
      }
  
      public class RootObject
      {
          public int id { get; set; }
          public List&lt;Buy&gt; buys { get; set; }
          public List&lt;Sell&gt; sells { get; set; }
      }
  </code></pre>
  
  <p>Form1.cs</p>
  
  <pre><code> private void button1_Click(object sender, EventArgs e)
          {
              RootObject RootObject = new RootObject();
              api_Request api_Request = new api_Request();
              richTextBox1.Text = api_Request.GetApi("https://api.guildwars2.com/v2/commerce/listings").id.ToString();
          }
  </code></pre>
  
  <p>In this json is a single ID, so this works fine. <a href="https://api.guildwars2.com/v2/commerce/listings/19684" rel="nofollow">https://api.guildwars2.com/v2/commerce/listings/19684</a></p>
  
  <p>But when retrieving multiple ID's like here, it breaks.
  <a href="https://api.guildwars2.com/v2/commerce/listings" rel="nofollow">https://api.guildwars2.com/v2/commerce/listings</a></p>