C# JSON Restful服务。无法从System.String强制转换或转换为System.Collections.Generic.List

C# JSON Restful服务。无法从System.String强制转换或转换为System.Collections.Generic.List,c#,json,vb.net,rest,json.net,C#,Json,Vb.net,Rest,Json.net,序列化和反序列化json时出现问题。我一直在寻找答案,虽然我在其他地方也看到了同样的问题,但没有一个答案对我有所帮助。我在使用newtonsoft或javascriptserializer时也遇到同样的问题 rest服务中的我的web方法 [OperationContract(Name="Customers")] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTempla

序列化和反序列化json时出现问题。我一直在寻找答案,虽然我在其他地方也看到了同样的问题,但没有一个答案对我有所帮助。我在使用newtonsoft或javascriptserializer时也遇到同样的问题

rest服务中的我的web方法

[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);
班级

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() : "";
    }
}
实际执行序列化的位

private String GetJSON(DataTable DT)
{
    try
    {
        List<Customer> customers = new List<Customer>();
        foreach (DataRow DR in DT.Rows)
        {
            customers.Add(new Customer(DR));
        }

        return JsonConvert.SerializeObject(customers);
    }
    catch
    {
        throw;
    }
}
我使用一个VB测试工具调用rest服务并反序列化返回的json

班级

Public Class Customer
    Public CustomerID As Int32
    Public CustomerCode As String
    Public CustomerName As String
End Class
方法

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
错误

{"Could not cast or convert from System.String to System.Collections.Generic.List`1[RESTTestHarness.Customer]."}

我使用了多种不同的方法,例如将bodystyle设置为Wrapped和使用根对象。似乎什么都不管用。我总是犯同样的错误。我相信这里的错误很简单,但我现在看不到它。

如果要返回通用json响应,需要更改服务操作契约以返回消息而不是字符串

然后,您的服务操作实现应该如下所示:

public System.ServiceModel.Channels.Message CreateJsonResponse()
{
  List<Customer> response = GetCustomers(); 

  string msg = JsonConvert.SerializeObject(response);

  return WebOperationContext.Current.CreateTextResponse(msg, "application/json; charset=utf-8", Encoding.UTF8);
}

我注意到在您的test harness Customer类中,您只有实例级别的成员,而不是公共属性,就像在方法中的Customer类中一样。是否可以将测试线束类更改为使用公共属性get;设置看看会发生什么?我确实试过,但也有同样的问题。谢谢你的回复,伊斯梅尔。我在路上回家,所以我早上就去看看。
public System.ServiceModel.Channels.Message CreateJsonResponse()
{
  List<Customer> response = GetCustomers(); 

  string msg = JsonConvert.SerializeObject(response);

  return WebOperationContext.Current.CreateTextResponse(msg, "application/json; charset=utf-8", Encoding.UTF8);
}