vb.net JSON Restful 服务。无法从 System.String 强制转换或转换为 System.Collections.Generic.List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25649221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON Restful service. Could not cast or convert from System.String to System.Collections.Generic.List
提问by Rob
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.
我在序列化和反序列化 json 时遇到问题。我已经寻找了一个答案,虽然我在其他地方看到过同样的问题,但没有一个答案对我有帮助。我在使用 newtonsoft 或 javascriptserializer 时遇到了同样的问题。
My web method in the rest service
我在其余服务中的网络方法
[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);
The class
班上
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() : "";
}
}
the bit that actually does the serializatiion
实际执行序列化的位
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;
}
}
So far everything seems ok. The service compiles and runs ok. when I test it in a browser I get the following result
到目前为止,一切似乎都很好。该服务编译并运行正常。当我在浏览器中测试它时,我得到以下结果
"[{\"CustomerID\":1,\"CustomerCode\":\"AMT-1\",\"CustomerName\":\".AMTDisp\"},{\"CustomerID\":2,\"CustomerCode\":\"COM-2\",\"CustomerName\":\".ComexDisp,_\"}]"
I have a VB test harness that I am using call the rest service and deserialize the returned json
我有一个 VB 测试工具,我正在使用它调用其余服务并反序列化返回的 json
The Class
班上
Public Class Customer
Public CustomerID As Int32
Public CustomerCode As String
Public CustomerName As String
End Class
the method
方法
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
The Error
错误
{"Could not cast or convert from System.String to System.Collections.Generic.List`1[RESTTestHarness.Customer]."}
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.
我使用了各种不同的方法,例如将 bodystyle 设置为 Wrapped 并拥有一个根对象。似乎没有任何效果。我总是得到同样的错误。我相信这里的错误很简单,但我现在看不到它。
采纳答案by Ismail Hawayel
if you want to return a generic json response, you need to change your service operation contract to return a Message not a string,
如果你想返回一个通用的 json 响应,你需要改变你的服务操作契约以返回一个 Message 而不是字符串,
your service operation implementation should then look something like:
您的服务操作实现应该如下所示:
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);
}

