MVC4错误:字符串的长度超过maxJsonLength属性上设置的值

时间:2015-02-06 18:01:24

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

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

  

“使用JSON进行序列化或反序列化时出错   JavaScriptSerializer。字符串的长度超过了设置的值   在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>

3 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。它对我来说很好。

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

答案 1 :(得分:0)

基于这篇文章 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配置appsetting部分。

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

答案 2 :(得分:0)

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

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

然后,在控制器动作中,我刚刚做了:

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

就是这样。