c# - 序列化或反序列化 JSON 期间的 ASP.NET MVC4 错误 - 大数据

标签 c# json asp.net-mvc-4 jsonserializer large-data

我写信给您时网站出现错误:使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了 maxJsonLength 属性上设置的值。

用到的技术:C#、. NET FW 4.5、ASP.NET MVC4 和 Lint to SQL、Kendo UI(用于显示结果的网格)。

我想返回(以 Json 形式)大量数据——实际上我有 50,000 条记录(超过 250,000 条记录不会出现)

我尝试扩大 maxJsonLength 并在未来启用 web.config 中的压缩 - 出现同样的错误:

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="10485760"/>
      </webServices>
      <scriptResourceHandler enableCompression="true" enableCaching="true"/>
    </scripting>
  </system.web.extensions>

接下来我尝试在 C# 类中重写 return 方法 - 出现同样的错误: a) 默认

public JsonResult GetResult()
{
  // execute query for get result
  var myBigData = from ......
      select new
      {
          .......
      };

  // return result
  return this.Json(myBigData, JsonRequestBehavior.AllowGet);
}

b) 重写(还是报错)

public JsonResult GetResult()
{
  // execute query for get result
  var myBigData = from ......
      select new
      {
          .......
      };

  // return result
  var jsonResult = Json(myBigData, JsonRequestBehavior.AllowGet);
  jsonResult.MaxJsonLength = int.MaxValue;
  return jsonResult;
}

为了消除错误,我尝试了较少数量的数据——一切正常: 测试哪个是正确的:6999 项中的 1 - 10 项

最好的问候, 彼得

注意:不幸的是,我进行了很多讨论(此处),但没有成功,所以对于任何重复的主题,我深表歉意。

最佳答案

seems来自 web.config 的值被忽略,并且根据 documentation您必须显式创建 JavascriptSerializer

的实例
public ContentResult GetResult() 
{
    var serializer = new JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var result = new ContentResult();
    result.Content = serializer.Serialize(data);
    result.ContentType = "text/json";
    return result;
}

至少对我有用:)

关于c# - 序列化或反序列化 JSON 期间的 ASP.NET MVC4 错误 - 大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15304258/

相关文章:

c# - 如何仅在 ConcurrentDictionary 存在的情况下通过键原子更新 ConcurrentDictionary 中的值

c# - 在通用列表中搜索项目时,我应该使用 LINQ 还是 Contains?

c# - 如何使用 AutoMapper 将 IEnumerable 映射到列表

json - 由于数据类型不匹配而获取 : argument 2 requires integral type error while parsing Json data Spark SQL

c# - PythonEngine.Initialize() 失败且没有任何错误消息

java - 从 JSON 中提取

json - 在扩展磁贴中显示 json 数组 - flutter

asp.net-mvc-4 - 为什么我们需要在 MVC 中使用 Web API? mvc中restful api的缺点是什么?

jquery - 如何使用 Jquery Ajax MVC 下载 Excel 文件和 PDF 文件

asp.net-mvc-4 - 缩小 css 文件如何在 mvc4 中工作?