关于 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值。

下面的代码段在序列化对象时字符串长度溢出
1 [HttpPost] 2 public ActionResult getData() 3 { 4 return Json(GetData()); 5 }
//GetData 返回类型为 List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
解决方案:
1.查阅资料发现有大佬说在web.config配置文件<configuration>节点下加入
1 <system.web.extensions> 2 <scripting> 3 <webServices> 4 <jsonSerialization maxJsonLength="1024000000" /> 5 </webServices> 6 </scripting> 7 </system.web.extensions>
但无法解决我出现的问题,还是过长。
2.不使用Json(),修改代码后如下后解决
1 [HttpPost] 2 public ActionResult getData() 3 { 4 JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); 5 jsSerializer.MaxJsonLength = Int32.MaxValue; 6 var result = new ContentResult 7 { 8 Content = jsSerializer.Serialize(GetData()), 9 ContentType = "application/json" 10 }; 11 return result; 12 }

浙公网安备 33010602011771号