title: Deserializing JSON to C# Class
body: <p>I'm calling an API that returns JSON data as follows (the array has around 500 items):</p>
  <pre><code>[
      [
          1571702400000,
          &quot;8197.28000000&quot;,
          &quot;8297.99000000&quot;,
          &quot;8000.00000000&quot;,
          &quot;8020.00000000&quot;,
          &quot;34651.82866300&quot;,
          1571788799999,
          &quot;283535483.41051363&quot;,
          384699,
          &quot;16767.03682400&quot;,
          &quot;137253195.95826291&quot;,
          &quot;0&quot;
      ],
      [
          1614816000000,
          &quot;50349.37000000&quot;,
          &quot;51773.88000000&quot;,
          &quot;49010.10000000&quot;,
          &quot;49653.85000000&quot;,
          &quot;25551.11905400&quot;,
          1614902399999,
          &quot;1280981565.05072601&quot;,
          731056,
          &quot;12197.45497900&quot;,
          &quot;611820740.21554123&quot;,
          &quot;0&quot;
      ]
  ]
  </code></pre>
  <p>I'm trying to parse this data using <strong>JsonConvert.DeserializeObject(result)</strong>.</p>
  <p>Both attempts I've tried (see below) produce the following error:</p>
  <p><strong>Error Message</strong></p>
  <p>Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '...+CandleSticks' because the type requires a JSON object (e.g. {&quot;name&quot;:&quot;value&quot;}) to deserialize correctly.</p>
  <p>To fix this error either change the JSON to a JSON object (e.g. {&quot;name&quot;:&quot;value&quot;}) 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.
  Path '', line 1, position 1.</p>
  <p><strong>Attempt #1:</strong></p>
  <pre><code>public class CandleStick
  {
      public List&lt;List&lt;object&gt;&gt; MyArray { get; set; } 
  }
  ...
  CandleStick myCandles = JsonConvert.DeserializeObject&lt;CandleStick&gt;(result);
  </code></pre>
  <p><strong>Attempt #2:</strong></p>
  <pre><code>public class CandleStick
  {
      public long openTime { get; set; }
      public string open { get; set; }
      public string high { get; set; }
      public string low { get; set; }
      public string close { get; set; }
      public string volume { get; set; }
      public long closeTime { get; set; }
      public string quoteAssetVolume { get; set; }
      public int numberOfTrades { get; set; }
      public string takerBuyBaseAssetVolume { get; set; }
      public string takerBuyQuoteAssetVolume { get; set; }
      public string ignore { get; set; }
  }
  ...
  List&lt;CandleStick&gt; myCandles = (List&lt;CandleStick&gt;)JsonConvert.DeserializeObject(result, typeof(List&lt;CandleStick&gt;));
  </code></pre>
  <p>How should I parse the JSON data correctly into a C# class?</p>
  <p><strong>EDIT - UPDATE</strong></p>
  <p>I can now deserialize the &quot;unhelpful&quot; JSON as follows:</p>
  <pre><code>var arrays = JsonConvert.DeserializeObject&lt;List&lt;List&lt;object&gt;&gt;&gt;(result);
              IEnumerable&lt;CandleStick&gt; candles = from values in arrays
                                 select new CandleStick
                                 {
                                     openTime = Convert.ToInt64(values[0]),
                                     open = Convert.ToString(values[1]),
                                     high = Convert.ToString(values[2]),
                                     low = Convert.ToString(values[3]),
                                     close = Convert.ToString(values[4]),
                                     volume = Convert.ToString(values[5]),
                                     closeTime = Convert.ToInt64(values[6]),
                                     quoteAssetVolume = Convert.ToString(values[7]),
                                     numberOfTrades = Convert.ToInt32(values[8]),
                                     takerBuyBaseAssetVolume = Convert.ToString(values[9]),
                                     takerBuyQuoteAssetVolume = Convert.ToString(values[10]),
                                     ignore = Convert.ToString(values[11])
                                 };
  
  CandleStick cd = candles.ElementAt(0);
  </code></pre>
  <p>When I try to reference the elements of candles, I get an error msg saying that the expected string was not in the correct format.</p>
  <p>Silly me! If I think about it more carefully the error message makes sense. My array was not converting one of the elements correctly. Now I can reference the data as expected - ElementAt(x) .ToList(), etc.</p>