Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't JsonConvert deserialize this object?

Tags:

json

c#

I have this JSON:

{  
    "CutCenterId":1,
    "Name":"Demo Cut Center",
    "Description":"Test",
    "IsAvailable":true,
    "E2CustomerId":"110000",
    "NumberOfMachines":2,
    "Machines":[]
}

I have the following POCO:

public class CutCenter
{
    int CutCenterId { get; set; }
    string Name { get; set; }
    string Description { get; set; }
    bool IsAvailable { get; set; }
    string E2CustomerId { get; set; }
    int NumberOfMachines { get; set; }
}

I try the following line of code where json is set to the above JSON and _cutCenter is a member variable.

_cutCenter = JsonConvert.DeserializeObject<CutCenter>(json);

After this _cutCenter is set to all defaults. Why? What am I doing wrong?

like image 940
Jordan Avatar asked Dec 20 '22 05:12

Jordan


1 Answers

Your members are all private. Try this.

public class CutCenter
{
    public int CutCenterId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool IsAvailable { get; set; }
    public string E2CustomerId { get; set; }
    public int NumberOfMachines { get; set; }
}
like image 141
Almo Avatar answered Dec 24 '22 03:12

Almo