C# Error When Deserialize Json for Object

Asked

Viewed 104 times

1

I’m making a mistake when I take the return of a REST service and try to deserialize to a C object#.

The REST Service returns me to the following structure(json):

{
"Marcacoes_Ponto_Response":
{
    "Item":
    {
        "Codigo_Retorno":"S"
        ,"Mensagem":"Operação realizada com sucesso"
        ,"Numero_Fabricacao_REP":"00099999999999990"
        ,"PIS":"12409663348"
        ,"Data_Marcacao":"20092019"
        ,"Hora_Marcacao":"093715"
    }
}

}

In C#code, I use the following code to deserialize an object:

var dadosFuncionario = JsonConvert.DeserializeObject<ResultPontoDigitalSAP>(responseFuncionario.Content.ReadAsStringAsync().Result);

The object definition used in the above codes follows below:

public class ResultPontoDigitalSAP
{
    public List<Marcacoes_Ponto_Response> Marcacoes_Ponto_Response { get; set; }
}

public class Marcacoes_Ponto_Response
{ 
    public string Codigo_Retorno { get; set; }
    public string Mensagem { get; set; }
    public string Numero_Fabricacao_REP { get; set; }
    public string PIS { get; set; }
    public string Data_Marcacao { get; set; }
    public string Hora_Marcacao { get; set; }

}

But when the JSON result is returned to application I take a C# JSON formatting error when I try to deserialize to the object set above.

Someone knows what I’m doing wrong?

  • Dear Oscar Filho here is Portuguese ... can translate your question?

  • Sorry, corrected. Thank you.

  • 1

    If it is the way it is in the question it is object object and not array object! it should be that. Good if reopen I answer

  • 1

    @Virgilionovic has reopened :)

1 answer

0


The layout of this is a objeto within another objeto and so in your question layout does not match because it is a array, example of how it would be:

public class Rootobject
{
    public Marcacoes_Ponto_Response Marcacoes_Ponto_Response { get; set; }
}

public class Marcacoes_Ponto_Response
{
    public Item Item { get; set; }
}

public class Item
{
    public string Codigo_Retorno { get; set; }
    public string Mensagem { get; set; }
    public string Numero_Fabricacao_REP { get; set; }
    public string PIS { get; set; }
    public string Data_Marcacao { get; set; }
    public string Hora_Marcacao { get; set; }
}

Use:

string json = responseFuncionario.Content.ReadAsStringAsync().Result;
var dadosFuncionario = JsonConvert.DeserializeObject<Rootobject>(json);
  • Friend, I did the test here and the resulting object of "Deserialize" remains NULL.

  • You did something wrong then ... because the layout is that same @Oscarfilho . Give a check usually the staff forget to put something ... or tell me what you have done now or you have not done the correct minimum example this usually happens here ... see the example is about the json that is in the question.

  • If you made a summary of Json ta ai the problem, it is this same JSON @Oscarfilho ?

  • 1

    Amigo, I had made a mess! Thanks a lot for the help!! Solved my problem and now I can better understand the relation JSON <> OBJECT C#.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.