【问题标题】:MVC4 error: The length of the string exceeds the value set on the maxJsonLength propertyMVC4 错误:字符串的长度超过了 maxJsonLength 属性上设置的值
【发布时间】:2015-04-06 23:01:37
【问题描述】:

我正在尝试使用 JSON JavaScriptSerializer 反序列化一个过大的字符串。我收到此错误。

" 使用 JSON 进行序列化或反序列化时出错 JavaScript 序列化器。字符串长度超过设置的值 在 maxJsonLength 属性上。”

HttpResponseMessage messge = client.GetAsync("ladders/get/d381241a0ad596d4bf02f441e75d1891fcc482e607c751e3978bc0adea6a9d99").Result;
string result = messge.Content.ReadAsStringAsync().Result;
string  description = result;

     JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
     var myobj = jsSerializer.Deserialize<List<List<CandidateResume>>>(description); 

这是我的课

 public class CandidateResume
        {

            public string name { get; set; }
            public string url { get; set; }
            public string summary { get; set; }
            public string role { get; set; }
            public string compensation { get; set; }
            public string education { get; set; }
            public string expertise { get; set; }
            public string years { get; set; }
            public string relocation { get; set; }
            public string resume { get; set; }
            public string resumeExtension { get; set; }
            public string resumeMimeType { get; set; }

        }

我已将 maxJsonLength 属性添加到 2147483644,但它仍然不起作用。

 <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483644"/>
      </webServices>
    </scripting>
  </system.web.extensions>

【问题讨论】:

  • 我已经这样解决了 JavaScriptSerializer jsSerializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 }; var myobj = jsSerializer.Deserialize>>(description);
  • 这个答案是最干净的。我知道你是几个月前发布的,也许你已经看过了:stackoverflow.com/questions/1151987/…

标签: c# json web-services asp.net-mvc-4


【解决方案1】:

我已经解决了这个问题。它对我来说工作正常。

JavaScriptSerializer jsSerializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 }; var myobj = jsSerializer.Deserialize<List<List<CandidateResume>>>(description);

【讨论】:

    【解决方案2】:

    根据这篇文章 Can I set an unlimited length for maxJsonLength in web.config?

    尝试以下配置,这比在代码中硬编码值更好。您的配置缺少一节

    <configuration> 
       <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="2147483647"/>
               </webServices>
           </scripting>
       </system.web.extensions>
    </configuration>
    

    已编辑,因为您似乎也在使用 mvc4,将以下内容添加到您的 web config appsetting 部分。

      <appSettings>
        <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
      </appSettings>
    

    【讨论】:

    • 那是用于网络服务的。这篇文章专门针对 MVC4。
    • @JimSpeaker 答案已更新,他的标签最初让我感到困惑,因为他说的是 Web 服务,但从外观上看,他只想要 mvc 解决方案。
    【解决方案3】:

    我的解决方案是创建这个类:

    public class LargeJsonResult : JsonResult
    {
        public LargeJsonResult()
            : base()
        {
            this.MaxJsonLength = Int32.MaxValue;
        }
    }
    

    然后,在控制器操作中,我只是这样做了:

    return new LargeJsonResult { Data = grid, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-26
      • 2015-03-27
      • 2012-08-06
      • 2015-05-30
      • 1970-01-01
      • 2018-04-25
      • 2017-08-31
      • 2013-10-07
      相关资源
      最近更新 更多