简体   繁体   中英

MaxJsonLength error in NewtonSoft JsonConvert.SerializeObject

I have a ASP.NET project, with some AJAX calling a webmethod, that would return JSON.

If the DataSet has about 100 lines, there is no problem. But with 1000 lines, it launch the error:

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

    [WebMethod(EnableSession = true)]
    public static string PublicWebMethod()
    {
            DataSet ds = new DataSet(); // in the reality do a mountrous query

            if (ds.Tables[0].Rows.Count > 0)
            {
                return JsonConvert.SerializeObject(clsUtil.ToArray(ds.Tables[0]));
            }
            else
            {
                return "false";
            }
        }
    }

How can I solve this by setting a configuration in the method?

I don't want to change the web.config

I had the similar issue and took a while to figure out the problem and fix. Please include the following code after

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM