我正在使用jQuery的自动完成功能。当我试图检索超过17000条记录的列表(每个记录不会超过10个字符长度)时,它超过了长度并抛出错误:

异常信息: 异常类型:InvalidOperationException 异常消息:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性设置的值。

我可以在web.config中设置maxJsonLength的无限长度吗?如果不是,我可以设置的最大长度是多少?


当前回答

如果你正在使用MVC 4,一定要看看这个答案。


如果你仍然收到错误:

在web.config中将maxJsonLength属性设置为最大值之后 你知道你的数据长度小于这个值 并且您没有使用web服务方法进行JavaScript序列化

你的问题可能是:

MaxJsonLength属性的值仅应用于异步通信层用于调用Web服务方法的内部JavaScriptSerializer实例。(MSDN: ScriptingJsonSerializationSection。MaxJsonLength属性)

基本上,当从web方法调用时,“内部”JavaScriptSerializer尊重maxJsonLength的值;直接使用JavaScriptSerializer(或通过MVC action-method/Controller使用)不尊重maxJsonLength属性,至少不尊重web.config的systemwebextension .scripting. webservices . jsonserialization部分。特别是,Controller.Json()方法不尊重配置设置!

作为一种变通方法,你可以在你的控制器(或任何地方)中执行以下操作:

var serializer = new JavaScriptSerializer();

// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;

var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
    Content = serializer.Serialize(resultData),
    ContentType = "application/json"
};
return result;

这个答案是我对这个asp.net论坛答案的解释。

其他回答

您可以在web中配置json请求的最大长度。配置文件:

<configuration>
    <system.web.extensions>
        <scripting>
            <webServices>
                <jsonSerialization maxJsonLength="....">
                </jsonSerialization>
            </webServices>
        </scripting>
    </system.web.extensions>
</configuration>

maxJsonLength的缺省值是102400。更多详细信息,请参见MSDN页面:http://msdn.microsoft.com/en-us/library/bb763183.aspx

注意:这个答案只适用于Web服务,如果你从Controller方法返回JSON,请确保你也阅读了下面的SO答案:https://stackoverflow.com/a/7207539/1246870


MaxJsonLength属性不能无限制,是一个默认为102400 (100k)的整数属性。

你可以在你的web.config中设置MaxJsonLength属性:

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 

如果,在实现上述添加到您的网页。config,你会得到一个“无法识别的配置部分system.web.extensions”。的错误,然后尝试将此添加到您的网页。<ConfigSections> section中的config:

            <sectionGroup name="system.web.extensions" type="System.Web.Extensions">
              <sectionGroup name="scripting" type="System.Web.Extensions">
                    <sectionGroup name="webServices" type="System.Web.Extensions">
                          <section name="jsonSerialization" type="System.Web.Extensions"/>
                    </sectionGroup>
              </sectionGroup>
        </sectionGroup>

修复ASP。NET MVC如果你想只修复导致问题的特定操作,请更改以下代码:

public JsonResult GetBigJson()
{
    var someBigObject = GetBigObject();
    return Json(someBigObject);
}

:

public JsonResult GetBigJson()
{
    var someBigObject = GetBigObject();
    return new JsonResult()
    {
        Data = someBigObject,
        JsonRequestBehavior = JsonRequestBehavior.DenyGet,
        MaxJsonLength = int.MaxValue
    };
}

功能应该是一样的,你可以返回更大的JSON作为响应。


基于ASP的解释。NET MVC源代码:你可以检查什么控制器。Json方法在ASP。NET MVC源代码

protected internal JsonResult Json(object data)
{
    return Json(data, null /* contentType */, null /* contentEncoding */, JsonRequestBehavior.DenyGet);
}

它正在调用其他控制器。Json方法:

protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonResult
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior
    };
}

其中传递的contentType和contentEncoding对象为空。所以基本上在控制器中调用return Json(object)相当于调用return new JsonResult {Data = object, JsonRequestBehavior = sonRequestBehavior。DenyGet}。您可以使用第二种形式并参数化JsonResult。

那么当你设置MaxJsonLength属性(默认为null)时会发生什么? 它传递给JavaScriptSerializer。MaxJsonLength属性,然后是JavaScriptSerializer。Serialize方法被调用:

JavaScriptSerializer serializer = new JavaScriptSerializer();
if (MaxJsonLength.HasValue)
{
    serializer.MaxJsonLength = MaxJsonLength.Value;
}

if (RecursionLimit.HasValue)
{
    serializer.RecursionLimit = RecursionLimit.Value;
}

response.Write(serializer.Serialize(Data));

当你不设置serializer的maxjsonlengt属性时,它会取默认值,也就是2MB。

如果你正在使用MVC 4,一定要看看这个答案。


如果你仍然收到错误:

在web.config中将maxJsonLength属性设置为最大值之后 你知道你的数据长度小于这个值 并且您没有使用web服务方法进行JavaScript序列化

你的问题可能是:

MaxJsonLength属性的值仅应用于异步通信层用于调用Web服务方法的内部JavaScriptSerializer实例。(MSDN: ScriptingJsonSerializationSection。MaxJsonLength属性)

基本上,当从web方法调用时,“内部”JavaScriptSerializer尊重maxJsonLength的值;直接使用JavaScriptSerializer(或通过MVC action-method/Controller使用)不尊重maxJsonLength属性,至少不尊重web.config的systemwebextension .scripting. webservices . jsonserialization部分。特别是,Controller.Json()方法不尊重配置设置!

作为一种变通方法,你可以在你的控制器(或任何地方)中执行以下操作:

var serializer = new JavaScriptSerializer();

// For simplicity just use Int32's max value.
// You could always read the value from the config section mentioned above.
serializer.MaxJsonLength = Int32.MaxValue;

var resultData = new { Value = "foo", Text = "var" };
var result = new ContentResult{
    Content = serializer.Serialize(resultData),
    ContentType = "application/json"
};
return result;

这个答案是我对这个asp.net论坛答案的解释。