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

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

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


当前回答

我们不需要任何服务器端更改。你只能通过web修改这个问题。配置文件 这对我很有帮助。试试这个

<appSettings>
 <add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
<add key="aspnet:UpdatePanelMaxScriptLength" value="2147483647" />
</appSettings>  

and   

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

其他回答

使用lib \ Newtonsoft.Json.dll

public string serializeObj(dynamic json) {        
    return JsonConvert.SerializeObject(json);
}

我建议设置为Int32.MaxValue。

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;

如果你从MVC中的MiniProfiler中得到这个错误,那么你可以通过设置属性MiniProfiler. settings . maxjsonresponsesize来增加这个值。默认情况下,该工具似乎忽略配置中设置的值。

MiniProfiler.Settings.MaxJsonResponseSize = 104857600;

礼貌mvc-mini-profiler。

问题是你是否真的需要返回17k条记录?您打算如何处理浏览器中的所有数据?用户无论如何都不会滚动17000行。

更好的方法是只检索“前几条”记录,并根据需要加载更多记录。

你不需要使用web.config 您可以在传递列表的catch值期间使用short属性 例如 像这样声明一个模型

public class BookModel
    {
        public decimal id { get; set; }  // 1 

        public string BN { get; set; } // 2 Book Name

        public string BC { get; set; } // 3 Bar Code Number

        public string BE { get; set; } // 4 Edition Name

        public string BAL { get; set; } // 5 Academic Level

        public string BCAT { get; set; } // 6 Category
}

这里我用的是短比例 公元前=条形码 BE=图书版本等等