15

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);
    }
1

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

If you would like to hardcode the maximum int value in maxJsonLength it is 2147483644.
serializer.MaxJsonLength = Int32.MaxValue; this line did the the trick for me! Thanks @zaki
I'm getting this error when request post with 700 and more length array list to controller.
@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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.