2015-04-01 103 views
4

我有此JSON:为什么JsonConvert不会反序列化这个对象?

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

我有以下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; } 
} 

我尝试下面的代码行,其中json被设定为上述JSON和_cutCenter是一个成员变量。

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

此后_cutCenter被设置为所有默认值。为什么?我究竟做错了什么?

+0

您的“机器”不属于您的课程。 – greenhoorn 2015-04-01 14:39:19

+0

你确定JSON是有效的。它看起来像缺少'['和']' - 相信我,这些小东西可能是你生活中的祸根。 – ppumkin 2015-04-01 14:39:38

+0

@ppumpkin,根据jsonformatter.curiousconcept.com它是有效的。 []是一个空数组。 – Jordan 2015-04-01 14:40:51

回答

11

您的会员均为私人会员。尝试这个。

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

呃!啊!抱歉。 – Jordan 2015-04-01 14:41:26

+0

容易犯错。 :) – Almo 2015-04-01 14:41:42

相关问题