【问题标题】:JavaScriptSerializer maxJsonLength exeeded超出 JavaScriptSerializer maxJsonLength
【发布时间】: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


【解决方案1】:

尝试在web.config中配置最大长度如下

<configuration>  
   <system.web.extensions>
   <scripting>
       <webServices>
           <!-- Update this value to set the max length -->
           <jsonSerialization maxJsonLength="2147483647" />
       </webServices>
   </scripting>
   </system.web.extensions>
</configuration>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多