Json response gets truncated in web api using asp.net core

≡放荡痞女 提交于 2020-01-21 06:20:07

问题


I have a web api that returns a json object for me to use on my website. the problem is this:

[
{
    "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

as you can see it gets truncated, it weights 20kb, string length is 2033, so I wanted to know if there is a way to increase the max size of the response somehow. I tried the MaxJsonLength inside the web.config but it is not working, might be because of the .net core aspect so I am kinda lost, why it is getting truncated.


回答1:


I don't know why the json response gets truncated at some point, but in my case (ASP.NET Core 2.0) I had to tell Newtonsoft.Json to ignore reference loops like so:

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



回答2:


One reason this can happen is if an error happens while serializing the object to JSON. For example a field marked as required that is not present in the data can cause this. The output simply stops and no exception is reported in this case.

Check that you can serialize the json object using JSonConvert.SerializeObject() before returning it and fix up any issues.




回答3:


For anyone who might end up here after seeing this when serving spatial types . . .

@IngoB 's answer made the issue go away, but then (along with the code-smell comment from @FailedUnitTest ringing true) there was a giant wad of stuff I wasn't expecting in the JSON for my simple Point. Setting ReferenceLoopHandling to Serialize and debugging allowed me to see details about what was happening, and eventually I ended up here.

Synopsis:

  1. Install the NetTopologySuite.IO.GeoJSON package
  2. Throw something like this in 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);
            }
        });
    



回答4:


I know this is a bit late and might be answered already by others in other posts, but I fixed this by serializing with a reader the sql query and returning the object which gets transformed instantly into JSON without returning the query as JSON from the start.




回答5:


The truncation at 2033 makes me think that you're only grabbing the first piece of what SQL Server is returning. You need to concatenate all the results together, e.g.:

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());
    }
}


来源:https://stackoverflow.com/questions/45655436/json-response-gets-truncated-in-web-api-using-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!