https://www.cnblogs.com/hongwei19930311/p/5382011.html
1、序列化:
以下代码在对象过大时会报错:进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。
//jsonObj比较大的时候会报错
var serializer = new JavaScriptSerializer();
return serializer.Serialize(jsonObj);
使用Newtonsoft.Json也有此问题,解决方案是设置MaxJsonLength:
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue; //设置为int的最大值
return serializer.Serialize(jsonObj);
2、ajax访问WebService:
以jQuery方式访问WebService,如果POST的数据过大,也会收到HTTP500错误,解决方法是在Web.config中设置一下maxJsonLength:
<system.web.extensions>
JavaScriptSerializer serializer = new JavaScriptSerializer();
ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
if (section != null)
{
serializer.MaxJsonLength = section.MaxJsonLength;
serializer.RecursionLimit = section.RecursionLimit;
}
问题解决了,但是小编还是觉得其中有疑问,就查询了更多帖子,发现一种更加完美的方式:
复制代码
public ActionResult GetLargeJsonResult()
{
return new ContentResult
{
Content = new JavaScriptSerializer { MaxJsonLength = Int32.MaxValue }.Serialize(listResult),
ContentType = "application/json"
};
}
复制代码
另外,发现一个讲解更加透彻的帖子,附上地址:http://www.cnblogs.com/artech/archive/2012/08/15/action-result-03.html