1

I'm currently using the DataContractJsonSerializer class but I cannot convert dates like 12/19/2013 12:00:00 AM to a C# DateTime Object.

The error that I'm getting says:

There was an error deserializing the object of type Uptivity.achievement. DateTime content '12/19/2013 12:00:00 AM' does not start with 'Date(' and end with ')' as required for JSON.

in:

    public static T DeserializeJSon<T>(string jsonString)
    {

        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(stream);
        return obj;
    }

I've been trying to define the datetime format like this:

     public static T DeserializeJSon<T>(string jsonString)
     {

       var settings = new DataContractJsonSerializerSettings
        {
            DateTimeFormat = new System.Runtime.Serialization.DateTimeFormat("G")
        };

        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T),settings);
        MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
        T obj = (T)ser.ReadObject(stream);
        return obj;
      }

But now I'm receiving:

There was an error deserializing the object of type Uptivity.achievement. String was not recognized as a valid DateTime.

Also this 12/19/2013 12:00:00 AM format like Date is the one that the json provider is delivering.

Any ideas?

KenD
  • 5,280
  • 7
  • 48
  • 85
user3149931
  • 107
  • 1
  • 9

1 Answers1

3

After some troubleshooting i come up with this as my solution:

using System.Web.Script.Serialization;

    public static T DeserializeJSon<T>(string jsonString)
    {

        JavaScriptSerializer ser = new JavaScriptSerializer();
        T obj = ser.Deserialize<T>(jsonString);

        return obj;
    }

And now it's running smoothly.

user3149931
  • 107
  • 1
  • 9