By Genish Parvadia


2014-11-14 08:54:42 8 Comments

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 comments

@Zaki 2014-11-14 09:01:21

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 2014-11-18 15:51:17

@DenishParvadia you are welcome and if it helped don't forget to mark it as answer :)

@Chad Kuehn 2016-11-29 13:23:35

If you would like to hardcode the maximum int value in maxJsonLength it is 2147483644.

@Aimal Khan 2017-05-18 15:37:54

serializer.MaxJsonLength = Int32.MaxValue; this line did the the trick for me! Thanks @zaki

@SPnL 2017-12-22 09:21:29

I'm getting this error when request post with 700 and more length array list to controller.

@mmangual83 2018-04-13 13:48:49

@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"

Related Questions

Sponsored Content

29 Answered Questions

[SOLVED] Can I set an unlimited length for maxJsonLength in web.config?

28 Answered Questions

[SOLVED] Deserialize JSON into C# dynamic object?

27 Answered Questions

[SOLVED] JavaScriptSerializer - JSON serialization of enum as string

41 Answered Questions

[SOLVED] How do I format a Microsoft JSON date?

1 Answered Questions

[SOLVED] Error during serialization or deserialization. JSON JavaScriptSerializer

1 Answered Questions

1 Answered Questions

3 Answered Questions

[SOLVED] Serialize bulk data by JSON JavaScriptSerializer in C#?

  • 2012-05-07 11:20:39
  • RAKESH HOLKAR
  • 992 View
  • 2 Score
  • 3 Answer
  • Tags:   c#

Sponsored Content