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
