java json 长度限制_解决MVC中JSON字符长度超出限制的异常
发布日期:2021-06-24 12:40:56 浏览次数:3 分类:技术文章

本文共 2925 字,大约阅读时间需要 9 分钟。

今日碰到了这么一个异常,异常信息如下:

Type : System.InvalidOperationException, mscorlib, Version=2.0.0.0,

Culture=neutral, PublicKeyToken=b77a5c561934e089

Message : 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串

的长度超过了为 maxJsonLength 属性设置的值。

Source : System.Web.Extensions

Help link :

Data : System.Collections.ListDictionaryInternal

TargetSite : Void Serialize(System.Object, System.Text.StringBuilder,

SerializationFormat)

Stack Trace :    在

System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj,

StringBuilder output, SerializationFormat serializationFormat)

在 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object

obj, SerializationFormat serializationFormat)

在 System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object

这个异常是在执行MVC中的JsonResult的时抛出的,根据异常的Message得知是序列化的字符串超出了maxJsonLength的限制。并得知这个属性是由JavaScriptSerializer提供的,因为MVC内置的JsonResult是用JavaScriptSerializer进行序列化的。在网上快速搜索了一下,碰到这个问题的童鞋不少,大部分推荐的解决的方法都是在web.config中加入以下配置,设置maxJsonLength的长度即可。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

在自动提示下,很顺畅的加上了几行代码,以为这是个简洁的解决方案,但是运行网站之后报以下错误:

分析器错误消息:无法识别的配置节 system.web.extensions。

这似乎又是碰到了一家人不认识的情况,既然无法识别为什么能带智能感知的方式输出,而且是已system.web开头的,按道理不会识别不出的。以为是拼写错误,经过进一步搜索之后,原来是缺乏了声明,加上对节点的声明即可(很大一串的内容)。

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngView Code

原来MVC框架内置的JsonResult代码中,在使用JavaScriptSerializer时,都是采用的默认值,没有从maxJsonLength读取值,即忽略了这个配置。要想使用配置中的值,只能自定义一个JsonResult,重写原JsonResult的ExecuteResult方法,于是定义一个ConfigurableJsonResult,代码如下:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.pngConfigurableJsonResult

1  public class ConfigurableJsonResult : JsonResult

2     {

3         public override void ExecuteResult(ControllerContext context)

4         {

5             if (context == null)

6             {

7                 throw new ArgumentNullException("context");

8             }

9             if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&

10                 String.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))

11             {

12                 throw new InvalidOperationException("This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.");

13             }

14

15             HttpResponseBase response = context.HttpContext.Response;

16

17             if (!String.IsNullOrEmpty(ContentType))

18             {

19                 response.ContentType = ContentType;

20             }

21             else

22             {

23                 response.ContentType = "application/json";

24             }

25             if (ContentEncoding != null)

26             {

27                 response.ContentEncoding = ContentEncoding;

28             }

29             if (Data != null)

30             {

31                 JavaScriptSerializer serializer = new JavaScriptSerializer();

32

33                 ScriptingJsonSerializationSection section = ConfigurationManager.GetSection("system.web.extensions/scripting/webServices/jsonSerialization") as ScriptingJsonSerializationSection;

34

35if (section != null)

36{

37serializer.MaxJsonLength = section.MaxJsonLength;

38serializer.RecursionLimit = section.RecursionLimit;

39}

40

41                 response.Write(serializer.Serialize(Data));

42             }

43         }

44     }

关键在红色标记的代码,读取配置的值。JavaScriptSerializer还有其他属性可配置,没有列出与实现,暂时够用。

这样在返回长字符内容的json结果时,直接替换原JsonResult即可,同时也兼顾了可配置的灵活性。

转载地址:https://blog.csdn.net/weixin_32804237/article/details/114115504 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:棋盘覆盖问题 java_java解决棋盘覆盖问题
下一篇:java中常见的设计模式_在Java中10种常见设计模式详细介绍

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月20日 21时30分27秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章