title: JSON Restful service. Could not cast or convert from System.String to System.Collections.Generic.List
body: <p>I have a problem serializing and deserializing json. I have searched for an answer and although Ive seen the same problem elsewhere, non of the answers have helped me. I'm having the same problem using newtonsoft or the javascriptserializer.</p>
  
  <p>My web method in the rest service</p>
  
  <pre><code>[OperationContract(Name="Customers")]
  [WebInvoke(Method = "GET",
          ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "json/Customer/Read/{CustomerID}/{CustomerCode}/{ParentCustomerID}/{CustomerName}")]
  String JSONReadCustomers(String CustomerID, String CustomerCode, String ParentCustomerID, String CustomerName);
  </code></pre>
  
  <p>The class</p>
  
  <pre><code>public class Customer
  {
      public Int32 CustomerID { get; set; }
      public String CustomerCode { get; set; }
      public String CustomerName { get; set; }
      public Customer(DataRow DR)
      {
          CustomerID = Convert.ToInt32(DR["CustomerID"]);
          CustomerCode = (DR["CustomerCode"] != null) ? DR["CustomerCode"].ToString() : "";
          CustomerName = (DR["CustomerName"] != null) ? DR["CustomerName"].ToString() : "";
      }
  }
  </code></pre>
  
  <p>the bit that actually does the serializatiion</p>
  
  <pre><code>private String GetJSON(DataTable DT)
  {
      try
      {
          List&lt;Customer&gt; customers = new List&lt;Customer&gt;();
          foreach (DataRow DR in DT.Rows)
          {
              customers.Add(new Customer(DR));
          }
  
          return JsonConvert.SerializeObject(customers);
      }
      catch
      {
          throw;
      }
  }
  </code></pre>
  
  <p>So far everything seems ok.
  The service compiles and runs ok. when I test it in a browser I get the following result</p>
  
  <pre><code>"[{\"CustomerID\":1,\"CustomerCode\":\"AMT-1\",\"CustomerName\":\".AMTDisp\"},{\"CustomerID\":2,\"CustomerCode\":\"COM-2\",\"CustomerName\":\".ComexDisp,_\"}]"
  </code></pre>
  
  <p>I have a VB test harness that I am using call the rest service and deserialize the returned json</p>
  
  <p>The Class</p>
  
  <pre><code>Public Class Customer
      Public CustomerID As Int32
      Public CustomerCode As String
      Public CustomerName As String
  End Class
  </code></pre>
  
  <p>the method</p>
  
  <pre><code>Private Function DeserializeJSON() As List(Of Customer)
  
      Dim request As WebRequest = WebRequest.Create(GetCustomerURL())
      request.Credentials = CredentialCache.DefaultCredentials
      Dim response As WebResponse = request.GetResponse()
      Dim dataStream As Stream = response.GetResponseStream()
  
      Dim reader As New StreamReader(dataStream)
      Dim responseJSON As String = reader.ReadToEnd()
  
      Dim customers As List(Of Customer) = JsonConvert.DeserializeObject(Of List(Of Customer))(responseJSON)
      Return customers
  
  End Function
  </code></pre>
  
  <p>The Error</p>
  
  <pre><code>{"Could not cast or convert from System.String to System.Collections.Generic.List`1[RESTTestHarness.Customer]."}
  </code></pre>
  
  <p>I have used a variety of different ways such as setting the bodystyle to Wrapped and having a root object. Nothing seems to work. I always get the same error. I believe the error is really simple here, but I just can't see it now.</p>