简体   繁体   English

NewtonSoft JsonConvert.SerializeObject中的MaxJsonLength错误

[英]MaxJsonLength error in NewtonSoft JsonConvert.SerializeObject

I have a ASP.NET project, with some AJAX calling a webmethod, that would return JSON. 我有一个ASP.NET项目,其中一些AJAX调用了webmethod,它将返回JSON。

If the DataSet has about 100 lines, there is no problem. 如果DataSet大约有100行,则没有问题。 But with 1000 lines, it launch the error: 但是,如果有1000行,它将引发错误:

Error during serialization or deserialization using JSON JavaScriptSerializer. 使用JSON JavaScriptSerializer进行序列化或反序列化时出错。 The size of the string exceeds the value set in the maxJsonLength property 字符串的大小超过在maxJsonLength属性中设置的值

    [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 我不想更改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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用JsonConvert.SerializeObject时发生可枚举错误 - Error for enumerable when using JsonConvert.SerializeObject JsonConvert.SerializeObject 抛出自引用错误 - JsonConvert.SerializeObject throwing self referencing error NewtonSoft JsonConvert.SerializeObject, \" 在序列化 DateTime 后添加 - NewtonSoft JsonConvert.SerializeObject, \" added after serializing DateTime 如果名称为大写字母,Newtonsoft JsonConvert.SerializeObject会忽略JsonProperty - Newtonsoft JsonConvert.SerializeObject ignoring JsonProperty if name is uppercase Newtonsoft.Json C# :: 格式化 JsonConvert.SerializeObject - Newtonsoft.Json C# :: Formatting JsonConvert.SerializeObject Newtonsoft.Json.JsonReaderException:在 JsonConvert.SerializeObject 之后解析 JsonConvert.DeserializeObject 时遇到意外字符 - Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing on JsonConvert.DeserializeObject after JsonConvert.SerializeObject JsonConvert.SerializeObject转义反斜杠 - JsonConvert.SerializeObject escaping backslash 尝试JsonConvert.SerializeObject时从“ ServerVersion”获取值时出错 - Error getting value from 'ServerVersion' when trying to JsonConvert.SerializeObject 为什么我不能在wcf服务中使用Newtonsoft.Json JsonConvert.SerializeObject? - Why I can't use Newtonsoft.Json JsonConvert.SerializeObject in wcf service? NewtonSoft JsonConvert.SerializeObject 确实将零序列化为负零( 0 序列化为 -0.0 ) - NewtonSoft JsonConvert.SerializeObject does serialize zero to negative zero ( 0 serialized like as -0.0 )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM