Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a JSON date info into a C# DateTime

Tags:

json

c#

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; }
  }
}

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;
}

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)\/

How do I get that into a C# DateTime instance?

Thanks!

like image 270
user70192 Avatar asked Feb 11 '11 13:02

user70192


2 Answers

Here are two options:

You can use the Deserialize method from System.Web.Script.Serialization.JavaScriptSerializer (in System.Web.Extensions.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)\/""";
like image 65
RP Niemeyer Avatar answered Oct 18 '22 09:10

RP Niemeyer


The reason the date is in this weird format is that DateTime is a primitive in WCF. Unfortunately, there is no universally standardized format for serializing dates and times in JSON -- various frameworks use various string formats.

The dilemma is that WCF needs to natively understand that a particular string over the wire is indeed a DateTime, not just another plain vanilla JSON string. Hence the strange format. As soon as DataContractJsonSerializer encounters a date starting with \/Date, it starts trying to parse it as a date.

Now, to answer your question, when you are sending a DateTime over the wire, it depends on if you're using a web browser client, a Silverlight client, or a WCF client.

A WCF client or a Silverlight 2+ client should NOT have problems with this -- they should auto-use the DataContractJsoNSerializer, and if they're not using it, you can plug in DCJS manually.

If you are using a web client, you can include the .js file that ships with ASP. NET AJAX (I believe it is called MicrosoftAspNetAjax.js, or MicrosoftAjax.cs, though the name may have changed). Its deserialize function will auto-parse these dates as well.

Hope that helps!

like image 34
krisragh MSFT Avatar answered Oct 18 '22 07:10

krisragh MSFT