title: JSON result can have array or single value - error parsing
body: <p>I'm trying to connect with an external API. It gives me the results in JSON and depending my search term, it could give zero, one or more-than-one results.</p>
  
  <p>I have mapped this to a C# class as follows:</p>
  
  <pre><code>Class Item
  - List&lt;ItemResult&gt; Results { get; set; }
  - Other properties, such as Name or Description (strings)
  </code></pre>
  
  <p>Whenever the JSON has an array of ItemResult (marked with []), it works fine, when the JSON has a single ItemResult (marked with {}), the parsing gives an error, because it expects a collection or array.</p>
  
  <p>My request is done as follows:</p>
  
  <pre><code>private T DoRequest&lt;T&gt;(string url)
  {
      try
      {
          var webRequest = (HttpWebRequest) WebRequest.Create(url);
          webRequest.Method = "GET";
          webRequest.Accept = "application/json";
          webRequest.UserAgent = UserAgent;
  
          var webResponse = webRequest.GetResponse();
          using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
          {
              var responseResult = streamReader.ReadToEnd();
              return JsonConvert.DeserializeObject&lt;T&gt;(CorrectJson(responseResult));
          }
      }
      catch (Exception e)
      {
          UtilityBL.LogError(e.Message);
          return default(T);
      }
  }
  </code></pre>
  
  <p>My question is: how can I make sure that this code handles both JSON results with an array of ItemResults as well as a single ItemResult?
  Perhaps I need to manually adjust the JSON result?</p>
  
  <p>Thanks!</p>
  
  <p>Edit: </p>
  
  <p>it's similar to this question, but instead of JQuery or Javascript, I need a .NET solution:
  <a href="https://stackoverflow.com/questions/3410587/how-to-read-both-single-object-array-of-objects-in-json-using-javascript-jquer">How to read both single object &amp; array of objects in json using javascript/jquery</a></p>
  
  <p>I have also tried following code, but it fails since JsonConvert tells me there now are 2 Album matches:</p>
  
  <pre><code>public class Albummatches
  {
      [JsonProperty("Album")]
      public List&lt;Album&gt; Albums { get; set; }
  
      [JsonProperty("Album")]
      public Album Album {
          set
          {
              Albums = new List&lt;Album&gt;();
              Albums.Add(value);
          }
      }
  }
  </code></pre>