1

When I make a request in the API it returns me a JSON in half. Maybe it's because of some transfer data limit.

For many data expected response:

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

For many data return of requisition:

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

Already tried using

jsonSerialization maxJsonLength="50000000"

But without success

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

Client:

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 .....
}
}

How do I identify if this error is occurring? ERRO: "Unterminated string. Expected delimiter: ". Path '[5066].Peso', line 1, position 1191900." in DeserializeObject

Rodrigo Martins
  • 119
  • 1
  • 7

3 Answers3

0

Usually this happens if your json or C# object have self referencing loop. for ignoring that see this

Tech Yogesh
  • 407
  • 2
  • 17
  • How do I identify if this error is occurring? (ReferenceLoopHandling) I do not get any type error. ERRO: "Unterminated string. Expected delimiter: ". Path '[5066].Peso', line 1, position 1191900." in DeserializeObject – Rodrigo Martins Oct 16 '18 at 13:28
0

I see a lot of problems in your Code and can't understand half, because it's in Spanish (I believe) but here is a code example that should take a id a dateTime and return a serialized Json object
Note: This requires the Newtonsoft.Json Nuget

    [HttpGet]
    public HttpResponseMessage GetFardosAlgodoeira(int id, string dateTime)
    {
        try
        {
            DateTime? data = null;
            if (dateTime is DateTime date)
                data = date;

            var result = _service.GetFardosAlgodoeira(id, dateTime.ToString(new CultureInfo("yyyy-MM-dd HH:mm:ss")));
            return Request.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(result));
        }
        catch (Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, e);
        }

    }

You will most probably not be able to copy/ paste this into your code for it to work, you'll probably need make some adjustments, I hope this helps

MindSwipe
  • 5,626
  • 19
  • 39
0

I think the problem is because of circular referencing objects,so you need to set the ReferenceLoopHandling to ignore if you use newtonsoft package

JsonConvert.SerializeObject(data, 
     Formatting.Indented, 
     new JsonSerializerSetting()
         {
             ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
         } 
 ));
  • How do I identify if this error is occurring? (ReferenceLoopHandling) I do not get any type error. ERRO: "Unterminated string. Expected delimiter: ". Path '[5066].Peso', line 1, position 1191900." in DeserializeObject – Rodrigo Martins Oct 16 '18 at 13:28