Doubt consuming RestService

kintelaFysegkintelaFyseg Member ✭✭

Hi

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},
{\"id\":\"AAGUI584\",\"name\":\"Farmacia\",\"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
{
[JsonProperty("id")]
public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("workStartDate")]
    public string WorkStartDate { get; set; }

    [JsonProperty("planningEndDate")]
    public string PlanningEndDate { get; set; }

    [JsonProperty("historicoDate")]
    public string HistoricoDate { get; set; }

    [JsonProperty("projectClosed")]
    public bool ProjectClosed { get; set; }

    [JsonProperty("phaseClosed")]
    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?
regards

Tagged:

Posts

  • 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
    Thanks

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