解析JSON最新信息成C#的DateTime [英] Parsing a JSON date info into a C# DateTime
问题描述
我有一个返回CLR对象的WCF服务。这个对象的定义如下:
I have a a WCF service that returns a CLR object. This object is defined as follows:
[DataContract]
public class Person
{
[DataMember]
public string FullName
{
get { return fullName; }
set { id = fullName; }
}
private string fullName = string.Empty;
[DataMember]
public DateTime BirthDate
{
get { return birthDate; }
set { birthDate = value; }
}
}
实例被创建并返回我的WCF服务。这项服务看起来如下:
Instances of this object are being created and returned from my WCF service. This service looks like the following:
[OperationContract]
[WebGet(UriTemplate = "/GetPersonByID/{id}", ResponseFormat = WebMessageFormat.Json)]
public Person GetPersonByID(string id)
{
Person person = FindPersonByID(id);
return person;
}
当我得到的回应回到我的应用程序,我可以成功地提取出全名值。但是,我还没有成功能的出生日期转换为C#DateTime对象在我的客户端应用程序。当转换为字符串,出生日期看起来像这样:
When I get the response back in my application, I can successfully extract the FullName value. However, I haven't successfully been able to convert the BirthDate to a C# DateTime object in my client application. When converted to a string, the BirthDate looks something like this:
\/Date(1297367252340-0500)\/
我如何得到成C#的DateTime实例?
How do I get that into a C# DateTime instance?
感谢
推荐答案
下面有两个选项:
您可以使用来自 System.Web.Script.Serialization的Deserialize方法。的JavaScriptSerializer资讯(System.Web.Extensions.dll)。
You can use the Deserialize method from System.Web.Script.Serialization.JavaScriptSerializer (in System.Web.Extensions.dll).
或者你可以从的 System.Runtime.Serialization.Json.DataContractJsonSerializer (在System.Runtime.Serialization.dll或.NET 3.5中System.ServiceModel.Web.dll)。
or you could use the ReadObject method from System.Runtime.Serialization.Json.DataContractJsonSerializer (in System.Runtime.Serialization.dll or in .NET 3.5 in System.ServiceModel.Web.dll).
请确保您的日期被包裹在引号,如:
Make sure that your date is wrapped in quotes like:
string dateString = @"""\/Date(1297367252340-0500)\/""";
这篇关于解析JSON最新信息成C#的DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!