title: Cannot deserialize the current JSON array into type from when response is xml
body: <p>I have the following response xml format from Microsoft:</p>
  
  <pre><code>&lt;SuggestResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Query&gt;state of decay 2&lt;/Query&gt;
      &lt;ResultSets&gt;
          &lt;SuggestResults&gt;
              &lt;Source&gt;dcatall-products&lt;/Source&gt;
              &lt;FromCache&gt;false&lt;/FromCache&gt;
              &lt;Type&gt;product&lt;/Type&gt;
              &lt;Suggests&gt;
                  &lt;Product&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Source&gt;Games&lt;/Source&gt;
                      &lt;Title&gt;State of Decay 2&lt;/Title&gt;
                      &lt;Url&gt;//www.microsoft.com/en-us/store/p/state-of-decay-2/9nt4x7p8b9nb&lt;/Url&gt;
                      &lt;ImageUrl&gt;//store-images.s-microsoft.com/image/apps.47366.14425140369408817.dca0bcd1-c06c-4345-8a00-fe5bfaff3665.45f69514-da1a-43cd-a1ec-109fe5e54d24?w=150&amp;amp;h=150&lt;/ImageUrl&gt;
                      &lt;Metas&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;BigCatalogId&lt;/Key&gt;
                              &lt;Value&gt;9NT4X7P8B9NB&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ProductType&lt;/Key&gt;
                              &lt;Value&gt;Game&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;Icon&lt;/Key&gt;
                              &lt;Value&gt;//store-images.s-microsoft.com/image/apps.47366.14425140369408817.dca0bcd1-c06c-4345-8a00-fe5bfaff3665.45f69514-da1a-43cd-a1ec-109fe5e54d24&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageHeight&lt;/Key&gt;
                              &lt;Value&gt;300&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageWidth&lt;/Key&gt;
                              &lt;Value&gt;300&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageType&lt;/Key&gt;
                              &lt;Value&gt;Logo&lt;/Value&gt;
                          &lt;/Meta&gt;
                      &lt;/Metas&gt;
                  &lt;/Product&gt;
                  &lt;Product&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Source&gt;Games&lt;/Source&gt;
                      &lt;Title&gt;State of Decay 2: Independence Pack&lt;/Title&gt;
                      &lt;Url&gt;//www.microsoft.com/en-us/store/p/state-of-decay-2-independence-pack/9n0jhbm3618s&lt;/Url&gt;
                      &lt;ImageUrl&gt;//store-images.s-microsoft.com/image/apps.19309.13920304356617547.62bf0ed6-7d21-4166-9bc0-99b6d7154bd1.e3af862c-7d10-4e9d-8f68-2c9a70d610b1?w=150&amp;amp;h=150&lt;/ImageUrl&gt;
                      &lt;Metas&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;BigCatalogId&lt;/Key&gt;
                              &lt;Value&gt;9N0JHBM3618S&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ProductType&lt;/Key&gt;
                              &lt;Value&gt;Durable&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;Icon&lt;/Key&gt;
                              &lt;Value&gt;//store-images.s-microsoft.com/image/apps.19309.13920304356617547.62bf0ed6-7d21-4166-9bc0-99b6d7154bd1.e3af862c-7d10-4e9d-8f68-2c9a70d610b1&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageHeight&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageWidth&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageType&lt;/Key&gt;
                              &lt;Value&gt;BoxArt&lt;/Value&gt;
                          &lt;/Meta&gt;
                      &lt;/Metas&gt;
                  &lt;/Product&gt;
                  &lt;Product&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Source&gt;Games&lt;/Source&gt;
                      &lt;Title&gt;State Of Decay 2: Ultimate Edition&lt;/Title&gt;
                      &lt;Url&gt;//www.microsoft.com/en-us/store/p/state-of-decay-2-ultimate-edition/9nsch45hz9hk&lt;/Url&gt;
                      &lt;ImageUrl&gt;//store-images.s-microsoft.com/image/apps.50602.14365785075596216.2000000000013039869.e8c6dd03-ff3a-48fb-a8ad-479a95448240?w=150&amp;amp;h=150&lt;/ImageUrl&gt;
                      &lt;Metas&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;BigCatalogId&lt;/Key&gt;
                              &lt;Value&gt;9NSCH45HZ9HK&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ProductType&lt;/Key&gt;
                              &lt;Value&gt;Game&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;Icon&lt;/Key&gt;
                              &lt;Value&gt;//store-images.s-microsoft.com/image/apps.50602.14365785075596216.2000000000013039869.e8c6dd03-ff3a-48fb-a8ad-479a95448240&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageHeight&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageWidth&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageType&lt;/Key&gt;
                              &lt;Value&gt;BoxArt&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;AppBgColor&lt;/Key&gt;
                              &lt;Value&gt;#FFFFFF&lt;/Value&gt;
                          &lt;/Meta&gt;
                      &lt;/Metas&gt;
                  &lt;/Product&gt;
                  &lt;Product&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Source&gt;Games&lt;/Source&gt;
                      &lt;Title&gt;State of Decay 2: Doomsday Pack&lt;/Title&gt;
                      &lt;Url&gt;//www.microsoft.com/en-us/store/p/state-of-decay-2-doomsday-pack/9pcqkkwf0zwp&lt;/Url&gt;
                      &lt;ImageUrl&gt;//store-images.s-microsoft.com/image/apps.6519.14169313509850372.ade11659-c080-431a-9d6b-6933c4264efb.20f0ebab-bd4f-41c3-a514-7f9347266072?w=150&amp;amp;h=150&lt;/ImageUrl&gt;
                      &lt;Metas&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;BigCatalogId&lt;/Key&gt;
                              &lt;Value&gt;9PCQKKWF0ZWP&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ProductType&lt;/Key&gt;
                              &lt;Value&gt;Durable&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;Icon&lt;/Key&gt;
                              &lt;Value&gt;//store-images.s-microsoft.com/image/apps.6519.14169313509850372.ade11659-c080-431a-9d6b-6933c4264efb.20f0ebab-bd4f-41c3-a514-7f9347266072&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageHeight&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageWidth&lt;/Key&gt;
                              &lt;Value&gt;1080&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageType&lt;/Key&gt;
                              &lt;Value&gt;BoxArt&lt;/Value&gt;
                          &lt;/Meta&gt;
                      &lt;/Metas&gt;
                  &lt;/Product&gt;
                  &lt;Product&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Source&gt;Devices&lt;/Source&gt;
                      &lt;Title&gt;State of Decay 2 for Xbox One&lt;/Title&gt;
                      &lt;Url&gt;//www.microsoft.com/en-us/store/p/state-of-decay-2-for-xbox-one/8p2fgp3bf0fc&lt;/Url&gt;
                      &lt;ImageUrl&gt;//img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Tc21?ver=3d93&amp;amp;w=272&amp;amp;h=223&amp;amp;o=f&amp;amp;m=6&amp;amp;f=jpg&lt;/ImageUrl&gt;
                      &lt;Metas&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;BigCatalogId&lt;/Key&gt;
                              &lt;Value&gt;8P2FGP3BF0FC&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ProductType&lt;/Key&gt;
                              &lt;Value&gt;Devices&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;Icon&lt;/Key&gt;
                              &lt;Value&gt;https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Tc21?ver=3d93&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageHeight&lt;/Key&gt;
                              &lt;Value&gt;1540&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageWidth&lt;/Key&gt;
                              &lt;Value&gt;1189&lt;/Value&gt;
                          &lt;/Meta&gt;
                          &lt;Meta&gt;
                              &lt;Key&gt;ImageType&lt;/Key&gt;
                              &lt;Value&gt;tile&lt;/Value&gt;
                          &lt;/Meta&gt;
                      &lt;/Metas&gt;
                  &lt;/Product&gt;
              &lt;/Suggests&gt;
          &lt;/SuggestResults&gt;
          &lt;SuggestResults&gt;
              &lt;Source&gt;microsoft-terms&lt;/Source&gt;
              &lt;FromCache&gt;false&lt;/FromCache&gt;
              &lt;Type&gt;term&lt;/Type&gt;
              &lt;Suggests&gt;
                  &lt;Term&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Txt&gt;state of decay year one survival edition&lt;/Txt&gt;
                  &lt;/Term&gt;
                  &lt;Term&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Txt&gt;state of decay year one survival edition for xbox one&lt;/Txt&gt;
                  &lt;/Term&gt;
                  &lt;Term&gt;
                      &lt;Curated&gt;false&lt;/Curated&gt;
                      &lt;Txt&gt;state of decay for xbox 360 download code&lt;/Txt&gt;
                  &lt;/Term&gt;
              &lt;/Suggests&gt;
          &lt;/SuggestResults&gt;
      &lt;/ResultSets&gt;
      &lt;ErrorSets&gt;
          &lt;SuggestErrorSet&gt;
              &lt;Source&gt;iris-products&lt;/Source&gt;
              &lt;Message&gt;Code: 2040. Message: Demand source returns error (Name: GN_ps, Error: No eligible content.).&lt;/Message&gt;
          &lt;/SuggestErrorSet&gt;
      &lt;/ErrorSets&gt;
  &lt;/SuggestResponse&gt;
  </code></pre>
  
  <p>Sometimes happens that SuggestResults to be a single element instead of array. So I created an <code>JsonProperty</code> as object and do trick in <code>ParsedSuggestResult</code> property.</p>
  
  <p>I have the following class:</p>
  
  <pre><code>public sealed class MsQueryXmlDetailResponse
      {
  
          public Suggestresponse SuggestResponse { get; set; }
  
          public class Suggestresponse
          {
              public string xmlnsxsd { get; set; }
              public string xmlnsxsi { get; set; }
              public string Query { get; set; }
              public Resultsets ResultSets { get; set; }
              public Errorsets ErrorSets { get; set; }
          }
  
          public class Resultsets
          {
              [JsonProperty("SuggestResults")]
              public object SuggestResultItems { get; set; }
  
              public Suggestresult[] ParsedSuggestResult {
                  get
                  {
                      if(SuggestResultItems is Suggestresult)
                      {
                          return new Suggestresult[] { SuggestResultItems as Suggestresult };
                      }
  
                      var result = SuggestResultItems as Suggestresult[];
                      if(result != null)
                      {
                          return result;
                      }
  
                      return new Suggestresult[] { JsonConvert.DeserializeObject&lt;Suggestresult&gt;(SuggestResultItems.ToString()) };
                  }
              }
          }
  
          public class Suggestresult
          {
              public string Source { get; set; }
              public string FromCache { get; set; }
              public string Type { get; set; }
              public Suggests Suggests { get; set; }
          }
  
          public class Suggests
          {
              public Product[] Product { get; set; }
              public Term[] Term { get; set; }
          }
  
          public class Product
          {
              public string Curated { get; set; }
              public string Source { get; set; }
              public string Title { get; set; }
              public string Url { get; set; }
              public string ImageUrl { get; set; }
              public Metas Metas { get; set; }
          }
  
          public class Metas
          {
              public Meta[] Meta { get; set; }
          }
  
          public class Meta
          {
              public string Key { get; set; }
              public string Value { get; set; }
          }
  
          public class Term
          {
              public string Curated { get; set; }
              public string Txt { get; set; }
          }
  
          public class Errorsets
          {
              public Suggesterrorset SuggestErrorSet { get; set; }
          }
  
          public class Suggesterrorset
          {
              public string Source { get; set; }
              public string Message { get; set; }
          }
  }
  </code></pre>
  
  <p>and I used Newtonsoft.Json to deserialize xml string format to a class (defined above):</p>
  
  <pre><code>var xmldoc = new XmlDocument();
  xmldoc.LoadXml(response);
  var fromXml = JsonConvert.SerializeXmlNode(xmldoc);
  MsQueryXmlDetailResponse data = JsonConvert.DeserializeObject&lt;MsQueryXmlDetailResponse&gt;(fromXml);
  </code></pre>
  
  <p>But I got:</p>
  
  <blockquote>
    <p>Cannot deserialize the current JSON array into type because the type
    requires a JSON object (e.g. {"name":"value"}) to deserialize
    correctly. 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>What should I do here ? If I do <code>SuggestResult[]</code> instead of <code>object</code> and xml response contains one SuggestResult element then error occur again.</p>