14

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

public string MemberDetail(string Code)
    {
        String res = "";
        SortedList sd = new SortedList();
        sd.Add("@mode", "MemberDetail");
        sd.Add("@Code", Code);
        SqlDataReader dr = erp.GetDataReaderSP("[Demo]", sd);
        DataTable dt = new DataTable();

        dt.Load(dr);
        Synchr[] obj = new Synchr[dt.Rows.Count];
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {

                obj[i].DemoName = Convert.ToInt32(dt.Rows[i]["Name"].ToString());
            }
        }

        return new JavaScriptSerializer().Serialize(obj);
    }
Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30
  • Possible duplicate of [MaxJsonLength exception in ASP.NET MVC during JavaScriptSerializer](http://stackoverflow.com/questions/5692836/maxjsonlength-exception-in-asp-net-mvc-during-javascriptserializer) – Sruit A.Suk Apr 21 '16 at 13:32

1 Answers1

31

I assume it is a web service that you are getting the data from (as your question is tagged "web-service"), change maxlength in web.config :

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

Or you can try the MaxJsonLength of JavaScriptSerializer :

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue; 
myObject obj = serializer.Deserialize<yourObject>(yourJsonString);
Zaki
  • 5,540
  • 7
  • 54
  • 91
  • 1
    If you would like to hardcode the maximum int value in `maxJsonLength` it is `2147483644`. – 2Yootz Nov 29 '16 at 13:23
  • serializer.MaxJsonLength = Int32.MaxValue; this line did the the trick for me! Thanks @zaki – Aimal Khan May 18 '17 at 15:37
  • I'm getting this error when request post with 700 and more length array list to controller. – SPnL Dec 22 '17 at 09:21
  • 1
    @Zaki this is an old post i know. but in my asp.net web app i am running into this issue. I tried both method and my application tells me "Error during serialization or deserialization using the JSON JavaScriptSerializer" – mmangual83 Apr 13 '18 at 13:48