字符串的长度超过 maxJsonLength 属性上设置的值
Posted
技术标签:
【中文标题】字符串的长度超过 maxJsonLength 属性上设置的值【英文标题】:length of the string exceeds the value set on the maxJsonLength property 【发布时间】:2015-03-27 23:05:00 【问题描述】:我有一个 .Net Web 服务 (.asmx),它将向我的客户端返回一个 Json 字符串。但是,我的一些数据确实很大,我偶尔会收到此错误。
字符串的长度超过了 maxJsonLength 属性设置的值。
我已将 maxJsonLength 属性更改为 2147483644,但它仍然不起作用。请帮忙...谢谢。
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void GetData(string login)
// throw an error on this line...
string result = new javascriptSerializer().Serialize(service.GetData(login));
Context.Response.Write(result);
【问题讨论】:
this question (and its answers) 能解决您的问题吗? George - @NextInLine 链接中的第二个答案是您想要的答案:将maxJsonLength
设置为new JavaScriptSerializer
的属性。链接中的第一个答案对您没有帮助。
您说您使用的是"Newtonsoft.Json",但在您的代码中您使用的是JavaScriptSerializer
。只是为了确认一下,您没有使用 Newtonsoft.Json,对吧?
@dbc:是的,你是对的。在这种情况下,我没有使用 Newtonsoft Json。我将它用于另一种方法。我很抱歉造成混乱。我会更新我的问题。谢谢:-)
@Ed Gibbs:嗨,Ed,谢谢。你的建议有效!我用的是第二个。
【参考方案1】:
感谢 Ed Gibbs 和 @NextInLine 的建议。我做了如下修复,现在它就像一个魅力。我还从我的 web.config 中删除了“system.web.extensions”部分
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void GetData(string login)
// when the amount of data return is huge
var serializer = new JavaScriptSerializer();
// we need to do this.
serializer.MaxJsonLength = Int32.MaxValue;
var result = serializer.Serialize(service.GetData(login));
Context.Response.Write(result);
【讨论】:
以上是关于字符串的长度超过 maxJsonLength 属性上设置的值的主要内容,如果未能解决你的问题,请参考以下文章