C# 将值 {null} 转换为输入 json 中的“System.DateTime”时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18534400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error converting value {null} to type 'System.DateTime' in input json
提问by Atif Qadri
This JsonSerializationException was thrown when I tried to input the following DateTime parameters in my Json :
当我尝试在我的 Json 中输入以下 DateTime 参数时抛出了这个 JsonSerializationException :
"Error converting value {null} to type 'System.DateTime' in input json"
“将值 {null} 转换为输入 json 中的“System.DateTime”时出错”
I have given the input here :
我在这里给出了输入:
string inputJSONString = "{....,\"StartDateFrom\":null,\"StartDateTo\":null,\"EndDateFrom\":null,\"EndDateTo\":null,\....}";
and deserialising using :
和反序列化使用:
scT = (SearchCriteriaTask)JsonConvert.DeserializeObject(inputJSONString , typeof(SearchCriteriaTask));
My json is correct , and I have also tried ("") values instead of null. I was not able to find proper solution elsewhere. Thanks.
我的 json 是正确的,我也尝试过 ("") 值而不是 null。我无法在其他地方找到合适的解决方案。谢谢。
If any part of code is needed, then please mention it.
如果需要代码的任何部分,请提及它。
采纳答案by SLaks
As the error is trying to tell you, .Net value types like DateTime
cannot hold nulls.
正如错误试图告诉您的那样,.Net 值类型(如DateTime
不能保存空值)。
If you want to allow nulls, use nullable types:
如果要允许空值,请使用可空类型:
DateTime? StartDateFrom { get; set; }