我有以下数据:
{“数据”:{“id”:“7IaWnXo”、“标题”:空、“描述”:空、“日期时间”:1397926970、“类型”:“图像/png”、“动画”:false、“宽度”:60、“高度”:60、“大小”:1277、“视图”:0、“带宽”:0、“喜爱”:false、“nsfw”:空、“KYIfVnHIWWTPifh”、“链接”:“http://i.imgur.com/7IaWnXo.png"},"success":true,"status":200}”
我试着把它序列化成这样:
public struct ImageInfoContainer
{
public ImageInfo data {get; set;}
bool success { get; set; }
string status { get; set; }
}
public struct ImageInfo
{
public string id {get; set;}
public string title { get; set; }
public string url { get; set; }
public string description {get; set;}
public string datetime {get; set;}
public string type {get; set;}
public string animated {get; set;}
public int width {get; set;}
public int height {get; set;}
public int size {get; set;}
public int views {get; set;}
public int bandwidth {get; set;}
public bool favourite {get; set;}
public bool nsfw {get; set;}
public string section {get; set;}
public string deletehash {get; set;}
public string link {get; set;}
}我得到了:
“System.InvalidOperationException”类型的异常发生在System.Web.Extensions.dll中,但未在用户代码中处理 附加信息:无法将null转换为值类型。
我做错了什么?
发布于 2014-04-19 17:34:07
在您的JSON数据中:nsfw为null。
但在ImageInfo结构中,nsfw被定义为boolean (it 不能为null,只有true或false)
你有两种可能性。
null用于nsfw。public bool? nsfw {get; set;}如果您选择第二个选项,这将允许您将true、false或null作为nsfw的值,并且您将不再有此错误。
Nullable<bool>和bool?都是相同的语法。
关于可空类型的更多信息
https://stackoverflow.com/questions/23173132
复制相似问题