简体   繁体   English

从C#CLR反序列化DateTime

[英]Deserialize DateTime From C# CLR

I have a json string in my CLR class like this: 我的CLR类中有一个json字符串,如下所示:

string parameteres = "{\"Parameter\":{\"personId\":\""+PersonId.ToString()+"\",\"date\":\""+Date.Value+"\"}}";

The CLR class calls The REST api and passes parameters to it. CLR类调用REST api并将参数传递给它。 In my REST api I deserialize parameter like this: 在我的REST API中,我反序列化参数是这样的:

JObject enterddata = JObject.Parse(jsoninput);
        string jsonparam = enterddata["Parameter"].ToString();
        var personId = new Guid();
        var date = new DateTime();
        try
        {
            JObject data = JObject.Parse(jsonparam);
            personId = new Guid(data["personId"].ToString());

            date = (DateTime)data["date"];

           //Other Codes Goes Here
        }
        catch (Exception ex)
        {

        }

and when I reach this line: 当我到达这一行:

date = (DateTime)data["date"]; 

The Exception occurred and doesn't convert it to DateTime. 发生异常,并且不会将其转换为DateTime。 How can I make it work? 我该如何运作?

您必须指定正确的日期格式,在这种情况下,ISO应该这样做

string parameteres = string.Format"{\"Parameter\":{\"personId\":\"{0}\",\"date\":\"{1:yyyy-MM-dd HH:mm:ss}\"}}",PersonId,Date.Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM