【问题标题】:Json response gets truncated in web api using asp.net core使用 asp.net 核心在 web api 中截断 Json 响应
【发布时间】:2018-08-27 08:14:20
【问题描述】:

我有一个 web api,它返回一个 json 对象供我在我的网站上使用。问题是这样的:

[
{
    "installment": 1,
    "date": "03\/01\/2016",
    "amount": "27.28",
    "status": "\"01BI000657\""
},
{
    "installment": 2,
    "date": "04\/01\/2016",
    "amount": "49.25",
    "status": "\"01BI000699\""
},
{
    "installment": 3,
    "date": "05\/01\/2016",
    "amount": "56.31",
    "status": "\"01BI000745\""
},
{
    "installment": 4,
    "date": "06\/01\/2016",
    "amount": "53.43",
    "status": "\"01BI000811\""
},
{
    "installment": 5,
    "date": "07\/01\/2016",
    "amount": "60.52",
    "status": "\"01EI279932\""
},
{
    "installment": 6,
    "date": "08\/01\/2016",
    "amount": "57.95",
    "status": "\"01BI000934\""
},
{
    "installment": 7,
    "date": "09\/01\/2016",
    "amount": "60.24",
    "status": "\"01BI001015\""
},
{
    "installment": 8,
    "date": "10\/01\/2016",
    "amount": "67.36",
    "status": "\"01EI298127\""
},
{
    "installment": 9,
    "date": "11\/01\/2016",
    "amount": "65.30",
    "status": "\"01BI001185\""
},
{
    "installment": 10,
    "date": "12\/01\/2016",
    "amount": "72.44",
    "status": "\"01BI001277\""
},
{
    "installment": 11,
    "date": "01\/01\/2017",
    "amount": "70.75",
    "status": "\"01BI001380\""
},
{
    "installment": 12,
    "date": "02\/01\/2017",
    "amount": "73.55",
    "status": "\"01BI001486\""
},
{
    "installment": 13,
    "date": "03\/01\/2017",
    "amount": "89.28",
    "status": "\"01BI001567\""
},
{
    "installment": 14,
    "date": "04\/01\/2017",
    "amount": "80.00",
    "status": "\"01BI001691\""
},
{
    "installment": 15,
    "date": "05\/01\/2017",
    "amount": "87.23",
    "status": "\"01BI001822\""
},
{
    "installment": 16,
    "date": "06\/01\/2017",
    "amount": "86.63",
    "status": "\"01BI002011\""
},
{
    "installment": 17,
    "date": "07\/01\/2017",
    "amount": "93.89",
    "status": "\"01BI002172\""
},
{
    "installment": 18,
    "date": "08\/01\/2017",
    "amount": "93.78",
    "status": "\"01BI002369\""
},
{
    "installment": 19,
    "date": "09\/01\/2017",
    "amount": "97.49",
    "status": "\"\""
},
{
    "installment": 20,
    "date": "10\/01\/2017",
    "amount": "104.81",
    "status": "\"\""
},
{
    "installment": 21,
    "date": "11\/01\/2017",
    "amount": "105.50",
    "status": "\"\""
},
{
    "installment": 22,
    "date": "12\/01\/2017",
    "amount": "112.87",
    "status": "\"\""
},
{
    "installment": 23,
    "date": "01\/01\/2018",
    "amount": "114.15",
    "status": "\"\""
},
{
    "installment": 24,
    "date": "02\/01\/2018",
    "amount": "118.67",
    "status": "\"\""
},
{
    "installment": 25,
    "date": "03\/01\/2018",
    "amount": "131.57",
    "status": "\"\""
},
{"ins

你可以看到它被截断了,它的重量为 20kb,字符串长度为 2033,所以我想知道是否有办法以某种方式增加响应的最大大小。我在 web.config 中尝试了 MaxJsonLength,但它不起作用,可能是因为 .net 核心方面,所以我有点迷茫,为什么它会被截断。

【问题讨论】:

    标签: json asp.net-web-api asp.net-core


    【解决方案1】:

    我的场景是

    1. .NET Core 3.1,
    2. 用于序列化的 Newtonsoft JSON,以及
    3. 用于更改响应正文(响应包装)的自定义中间件。

    JSON response breaks in ASP.NET Core 3.1 Web API with custom response wrapper 描述的解决方案对我有用。

    另外,https://github.com/dotnet/core/issues/1240 也有一些有用的讨论。

    【讨论】:

      【解决方案2】:

      发生这种情况的一个原因是如果在将对象序列化为 JSON 时发生错误。例如,标记为必填但数据中不存在的字段可能会导致此问题。输出只是停止,在这种情况下不报告异常。

      在返回之前检查您是否可以使用JSonConvert.SerializeObject() 序列化 json 对象并修复任何问题。

      【讨论】:

        【解决方案3】:

        2033 处的截断让我觉得您只是抓住了 SQL Server 返回的第一部分。 You need to concatenate all the results together,例如:

        var queryWithForJson = "SELECT ... FOR JSON";
        var conn = new SqlConnection("<connection string>");
        var cmd = new SqlCommand(queryWithForJson, conn);
        conn.Open();
        var jsonResult = new StringBuilder();
        var reader = cmd.ExecuteReader();
        if (!reader.HasRows)
        {
            jsonResult.Append("[]");
        }
        else
        {
            while (reader.Read())
            {
                jsonResult.Append(reader.GetValue(0).ToString());
            }
        }
        

        【讨论】:

          【解决方案4】:

          我知道这有点晚了,其他帖子中的其他人可能已经回答了这个问题,但我通过使用阅读器序列化 sql 查询并返回立即转换为 JSON 的对象而不将查询返回为 JSON 来解决此问题开始。

          【讨论】:

            【解决方案5】:

            适用于在提供 空间类型 时看到此内容的任何人。 . .

            @IngoB 的回答使问题消失了,但随后(连同来自@FailedUnitTest 的代码气味评论响起)对于我的简单@987654321,我在 JSON 中没有预料到的一大堆东西@。将ReferenceLoopHandling 设置为Serialize 并进行调试让我可以看到正在发生的事情的详细信息,最终我得到了here

            简介:

            1. 安装NetTopologySuite.IO.GeoJSON package
            2. 在 ConfigureServices() 中抛出类似的东西:

              services.AddMvc(options =>
                  {
                      options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Point)));
                  })
                  .AddJsonOptions(options =>
                  {
                      foreach (var converter in GeoJsonSerializer.Create(new GeometryFactory(new PrecisionModel(), 4326)).Converters)
                      {
                          options.SerializerSettings.Converters.Add(converter);
                      }
                  });
              

            【讨论】:

              【解决方案6】:

              我不知道为什么 json 响应在某些时候会被截断,但在我的情况下(ASP.NET Core 2.0)我不得不告诉 Newtonsoft.Json 忽略这样的引用循环:

              public void ConfigureServices(IServiceCollection services)
              {
                  services.AddMvc()
                      .AddJsonOptions(
                          options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                      );
              }
              

              【讨论】:

              • 这对我来说是代码异味的标志。为什么这个模型中有一个 ReferenceLoop?这是 ViewModel + AutoMapper 的好案例吗?
              • 我今天遇到了这个问题,这解决了我的问题。我很震惊更多的人没有遇到这个。感谢您的提示!
              • 最佳答案!!它确实解决了问题!
              猜你喜欢
              • 2020-05-03
              • 2020-09-19
              • 1970-01-01
              • 1970-01-01
              • 2022-11-04
              • 1970-01-01
              • 2020-01-24
              • 2023-03-20
              相关资源
              最近更新 更多