我正在使用JSON.NET进行反序列化,但是将对象实例化并将其所有属性设置为null。
JSON
{
"verify-purchase":
{
"item_name":"Pipeline.NET Task Scheduler",
"item_id":"1111111",
"created_at":"Wed Jun 12 15:56:02 +1000 2013",
"buyer":"xxxxxxxx",
"licence":"Regular License"
}
}
C#Class
public class VerifyPurchase
{
[JsonProperty("item_name")]
public string ItemName { get; set; }
[JsonProperty("item_id")]
public string ItemId { get; set; }
[JsonProperty("created_at")]
public string CreatedAt { get; set; }
[JsonProperty("buyer")]
public string Buyer { get; set; }
[JsonProperty("licence")]
public string Licence { get; set; }
}
C#反序列化
var purchase = JsonConvert.DeserializeObject<VerifyPurchase>(jsonText);
这看起来很简单。这导致NULL属性出了什么问题?
答案 0 :(得分:1)
你可以有一个包装类,允许你指定verify-purchase
属性:
public class Wrapper
{
[JsonProperty("verify-purchase")]
public VerifyPurchase Purchase { get; set; }
}
你要反序列化:
var wrapper = JsonConvert.DeserializeObject<Wrapper>(jsonText);
VerifyPurchase purchase = wrapper.Purchase;
// the purchase.* properties should be assigned at this stage