chhaya_patel chhaya_patel - 2 months ago 11
JSON Question

Getting error while serializing using JavaScriptSerializer in c#

When I am serializing using JavaScriptSerializer in wcf service it gives me below error for defined code.

Code:

DataCollection<Entity> detailqueryentityCollection = _serviceProxy.RetrieveMultiple(detailquery).Entities;
if (detailqueryentityCollection.Count > 0)
{
listdata = new JavaScriptSerializer().Serialize(detailqueryentityCollection);
}


Error:


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


I also add below code in web.config but it doesn't work.

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


Please suggest me any solutions.

Answer

When instantiating the class yourself, you need to add the value to the MaxJsonLength property of the class:

var jss = new JavaScriptSerializer();
jss.MaxJsonLength = 500000000;
listData = jss.Serialize(detailqueryentityCollection);
Comments