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>
- <scripting>
- <webServices>
- <!--单位为字节-->
- <jsonSerializationmaxJsonLength="1024000"/>
- </webServices>
- </scripting>
- </system.web.extensions>
//访问调用方法
JavaScriptSerializer serializer = new JavaScriptSerializer();
32
33 ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;
34
35 if (section != null)
36 {
37 serializer.MaxJsonLength = section.MaxJsonLength;
38 serializer.RecursionLimit = section.RecursionLimit;
39 }