pix*_*ist 5 c# exception javascriptserializer
我有以下数据:
{ "数据":{ "ID": "7IaWnXo", "标题":空, "说明":NULL, "日期时间":1397926970, "类型": "图像/ PNG", "动画片":假,"宽度"60," 高度 ":60," 大小 ":1277," 意见 ":0," 带宽 ":0," 最爱 ":假的," NSFW ":空," 节 ":空," deletehash": "KYIfVnHIWWTPifh", "链接": "http://i.imgur.com/7IaWnXo.png"}, "成功":真实的, "身份":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;}
}
Run Code Online (Sandbox Code Playgroud)
我得到了:
System.Web.Extensions.dll中出现"System.InvalidOperationException"类型的异常,但未在用户代码中处理
附加信息:无法将null转换为值类型.
我究竟做错了什么?
Jus*_*man 14
在您的JSON数据中:nsfw为null.
但在ImageInfostruct中,nsfw被定义为a boolean(它不能为null,仅为true或false)
你有两种可能性.
null的nsfw.public bool? nsfw {get; set;} 如果你把第二个选项,这将让你有true,false或null作为一个值nsfw,你不会有这样的错误了.
这两个Nullable<bool>和bool?是相同的语法.
有关Nullable Types的更多信息