为什么JsonConvert不会反序列化这个对象?
问题描述:
我有此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
被设置为所有默认值。为什么?我究竟做错了什么?
答
您的会员均为私人会员。尝试这个。
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; }
}
您的“机器”不属于您的课程。 – greenhoorn 2015-04-01 14:39:19
你确定JSON是有效的。它看起来像缺少'['和']' - 相信我,这些小东西可能是你生活中的祸根。 – ppumkin 2015-04-01 14:39:38
@ppumpkin,根据jsonformatter.curiousconcept.com它是有效的。 []是一个空数组。 – Jordan 2015-04-01 14:40:51