【发布时间】:2016-01-22 23:17:29
【问题描述】:
我正在 Visual Studio 2013 中用 C# 创建一个 Web 服务。 我已连接到数据库并使用以下代码返回 json。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCustomer()
{
var json = "";
var customer = from result in dc.Auto_Kada_SIA_Customers
select result;
JavaScriptSerializer jss = new JavaScriptSerializer();
jss.MaxJsonLength = Int32.MaxValue;
json = jss.Serialize(customer);
int t = json.Length;
return json;
}
但是当我尝试使用它时,我得到以下错误
{
"Message": "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.",
"StackTrace": " at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)",
"ExceptionType": "System.InvalidOperationException"
}
如果是这样的话我会没问题,但 MaxJsonLentgh 设置为 2,147,483,647 并且 json.Length 是 21,460,284。
问题是什么,我该如何解决?
【问题讨论】:
-
不是用上面的注解自动将对象序列化为json吗?试试
public Customer[] GetCustomer...return dc.Auto_Kada_SIA_Customers; -
没有名为 Customer 的类,因为我使用的是 DataClasses
标签: c# json web-services visual-studio-2013