API返回JSON返回一半

时间:2018-10-16 11:43:44

标签: c# json api

当我在API中发出请求时,它会返回一半的JSON。也许是因为传输数据限制。

对于许多数据的预期响应:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

对于许多数据退回申请:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",

已经尝试使用

jsonSerialization maxJsonLength="50000000"

但没有成功

请求API:

[HttpGet("Sincronizar/{algodoeiraId}")]
public JsonResult GetFardosAlgodoeira(int algodoeiraId, [FromHeader] string DUMANUT)
{
    try
    {
        DateTime? data = null;
        if (!String.IsNullOrEmpty(DUMANUT))
        {
            data = DateTime.ParseExact(DUMANUT, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture).AddMinutes(-20);
        }

        _service = new FardoService(GetClientConnectionString());
        var result = _service.GetFardosAlgodoeira(algodoeiraId, data?.ToString("yyyy-MM-dd HH:mm:ss"));
        return Json(result); // result returns me the desired objects, but where I get the data comes in half
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        return Json(e);
    }
}

客户:

public async Task<RequestResult<T>> Get<T>(string endPoint, DateTimeOffset? dumanut = null, bool mostrarAlertaSemInternet = true)
{
try
{
client.Timeout = TimeSpan.FromMinutes(20);

var request = new HttpRequestMessage(HttpMethod.Get, new Uri(endPoint));
request.Headers.Add("DUMANUT", dumanut?.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss"));

var response = await client.SendAsync(request);

if (response != null && response.IsSuccessStatusCode)
{
var resultString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<T>(resultString); // ERROR HERE %%%%%%%%
return new RequestResult<T> { Succeeded = true, ObjetoRetorno = result };
}

return null;
}
catch (Exception ex)
{
//Exception .....
}
}

如何确定是否发生此错误? ERRO:“未终止的字符串。预期的分隔符:”。 DeserializeObject中的路径'[5066] .Peso',第1行,位置1191900。“

3 个答案:

答案 0 :(得分:0)

通常,如果您的json或C#对象具有自引用循环,就会发生这种情况。请忽略this

答案 1 :(得分:0)

我在您的代码中发现了很多问题,并且一半都看不懂,因为它是西班牙语的(我相信),但这是一个代码示例,应使用RequestMappingInfo info = RequestMappingInfo .paths(requestMapping(...)) .methods(RequestMethod.GET) .build(); id并返回序列化的Json对象
注意:,这需要Newtonsoft.Json Nuget

dateTime

您很可能无法将其复制/粘贴到代码中以使其正常工作,您可能需要进行一些调整,希望对您有所帮助

答案 2 :(得分:0)

我认为问题是由于循环引用对象,因此您需要设置 如果您使用newtonsoft软件包

,则ReferenceLoopHandling可以忽略
JsonConvert.SerializeObject(data, 
     Formatting.Indented, 
     new JsonSerializerSetting()
         {
             ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
         } 
 ));