Jordan
Jordan

Reputation: 9911

Why won't JsonConvert deserialize this object?

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?

Upvotes: 4

Views: 138

Answers (1)

Almo
Almo

Reputation: 15871

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

Upvotes: 11

Related Questions