Friday, August 15, 2008

JSonSerialization Error Solution

Sometime when working with ScriptSevice and ScriptMethod in ASP.net webservice following error may occur.

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

Reason for this is JavaScript Serialization setting . This allow maximum 2097152 character by default. It method return string larger then this then it throw above error.

Solution for this is to put following configuration in web.config file.

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




In above configuration instead you can set maxJsonLength to certain value so it allow larger string.

Maximum value for that field is 2147483647 as this property is System.Int32.

More information you can find it on following location.
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength.aspx

4 comments:

Anonymous said...

Just what I needed. Thanks!

Anonymous said...

Thanks for this. Hit the same problem

skh said...

Thanks!

Anonymous said...

thanks buddy, its really helpful :)

thanx