Doubt consuming RestService

kintelaFysegkintelaFyseg Member ✭✭


I'm consuming my webAPI and I receive this JSON object

"[{\"id\":\"AAGUI577\",\"name\":\"DO C/ Barranco\",\"workStartDate\":\"0001-01-01T00:00:00\",\"planningEndDate\":\"0001-01-01T00:00:00\",\"historicoDate\":\"0001-01-01T00:00:00\",\"projectClosed\":true,\"phaseClosed\":false},
That I want to map to this model
public class ProyectoData
public string Id { get; set; }

    public string Name { get; set; }

    public string WorkStartDate { get; set; }

    public string PlanningEndDate { get; set; }

    public string HistoricoDate { get; set; }

    public bool ProjectClosed { get; set; }

    public bool PhaseClosed { get; set; }

But when I execute this
string content = await response.Content.ReadAsStringAsync();
proyectoData = JsonConvert.DeserializeObject(content);

proyectoData is null

Any idea please?



  • JohnHJohnH GBMember ✭✭✭✭✭

    Just an observation, but shouldn't it be:
    proyectoData = JsonConvert.DeserializeObject<ProyectoData>(content);

  • kintelaFysegkintelaFyseg Member ✭✭
    edited May 17

    Yes sorry I have copied wrong my code in the post

    This is the code where proyectoData is null

  • FaizalSaidaliFaizalSaidali USMember ✭✭✭

    @kintelaFyseg I think your JSON string is list of ProyectoData model, right?
    Now you have reserialize with the model, you just change the code like this.

    proyectoData = JsonConvert.DeserializeObject<List<ProyectoData>>(content);

    I think it will work fine.

  • kintelaFysegkintelaFyseg Member ✭✭

    Yes this is the solution thanks you very much

Sign In or Register to comment.