title: JSON.NET Deserialization - Single Result vs Array
body: <p>I am having trouble trying to determine how to make my Serialization Properly be able to access a single result, as well as an array.</p>
  
  <p>When I make a REST call looking for something on a server, sometimes it will return an Array of models, but if the search results only have a single model, it will not be returned as an error.  This is when I get an exception that I cannot deserialize because the Object Property is expecting an array, but is instead receiving a single object.</p>
  
  <p>Is there a way to define my class so that it can handle a single object of type ns1.models when that is returned instead of an array of objects?</p>
  
  <pre><code>[JsonObject]
  public class Responses
  {
      [JsonProperty(PropertyName = "ns1.model")]
      public List&lt;Model&gt; Model { get; set; }
  }
  </code></pre>
  
  <p><strong>Response that can be deserialized:</strong></p>
  
  <pre><code>{"ns1.model":[
    {"@mh":"0x20e800","ns1.attribute":{"@id":"0x1006e","$":"servername"}},
    {"@mh":"0x21a400","ns1.attribute":{"@id":"0x1006e","$":"servername2"}}
  ]}
  </code></pre>
  
  <p><strong>Response that cannot be serialized (because JSON includes only a singe "ns1.model"):</strong></p>
  
  <pre><code>{"ns1.model":
     {"@mh":"0x20e800","ns1.attribute":{"@id":"0x1006e","$":"servername"}}
  }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>Newtonsoft.Json.JsonSerializationException was unhandled   HResult=-2146233088   Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ConsoleApplication1.Model]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List&lt;T&gt;) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path '['ns1.model-response-list'].['ns1.model-responses'].['ns1.model'].@mh', line 1, position 130
  </code></pre>