在输入json中将值{null}转换为类型'System.DateTime'时出错 [英] Error converting value {null} to type 'System.DateTime' in input json

查看:906
本文介绍了在输入json中将值{null}转换为类型'System.DateTime'时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在我的Json中输入以下DateTime参数时,抛出此JsonSerializationException:

This JsonSerializationException was thrown when I tried to input the following DateTime parameters in my Json :


将值{null}转换为错误在输入json中输入 System.DateTime

"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。
我在其他地方找不到合适的解决方案。谢谢。

My json is correct , and I have also tried ("") values instead of null. I was not able to find proper solution elsewhere. Thanks.

如果需要代码的任何部分,请提及。

If any part of code is needed, then please mention it.

推荐答案

由于错误正试图告诉您,.net值类型,例如 DateTime 不能包含空值。

As the error is trying to tell you, .Net value types like DateTime cannot hold nulls.

如果要允许为空,请使用可为空的类型:

If you want to allow nulls, use nullable types:

DateTime? StartDateFrom { get; set; }

这篇关于在输入json中将值{null}转换为类型'System.DateTime'时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆