当我尝试在我的 Json 中输入以下 DateTime 参数时抛出此 JsonSerializationException:
"Error converting value {null} to type 'System.DateTime' in input json"
我在这里给出了输入:
string inputJSONString = "{....,\"StartDateFrom\":null,\"StartDateTo\":null,\"EndDateFrom\":null,\"EndDateTo\":null,\....}";
反序列化使用:
scT = (SearchCriteriaTask)JsonConvert.DeserializeObject(inputJSONString , typeof(SearchCriteriaTask));
我的 json 是正确的,我也试过 ("") 值而不是 null。 我无法在其他地方找到合适的解决方案。谢谢。
如果需要任何部分代码,请提出。
最佳答案
正如该错误试图告诉您的那样,DateTime
等 .Net 值类型不能包含空值。
如果要允许空值,请使用可空类型:
DateTime? StartDateFrom { get; set; }
关于c# - 在输入 json 中将值 {null} 转换为类型 'System.DateTime' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18534400/