2

I have the following DateTime field in business entity c# :

public DateTime BirthDate { get; set; }

I got the following message when I call the API passing the entity object as JSON:

DateTime content '2009-02-15T00:00:00Z' does not start with '\/Date(' and end with ')\/' as required for JSON.

I searched the web tried many formats but does not work with me!

The error message is clear! but I have more than an hour trying to send a request to my API. Please do not vote down my post! I did my best.

This is the JSON object I sent via postman:

{
  "patient": {
"Number": 20012,
"FirstName": "ِAnas",
"LastName": "Tina",
"BirthDate":"1986-12-29",
"Phone": "000000",
"Mobile": "00000",
"Address": "Damas",
"Job": "Developer",
"Note": "This is a note",
"GenderId": 1
  }
}

[DataContract]    
public class Patient
{
    [Description("Patient's Id in ECMS database")]
    [DataMember(Order = 1)]
    public int Id { get; set; }

    [Description("Patient's unique number")]
    [DataMember(Order = 2)]
    public int Number { get; set; }

    [Description("Patient's first name")]
    [DataMember(Order = 3)]
    public string FirstName { get; set; }

    [Description("Patient's last name")]
    [DataMember(Order = 4)]
    public string LastName { get; set; }

    [Description("Patient's birth date")]
    [DataMember(Order = 5)]
    public DateTime BirthDate { get; set; }
}
9
  • Can you provide the code that generates JSON? Commented Nov 4, 2019 at 3:23
  • Try as timestamp /Date(1379048144000)/ Commented Nov 4, 2019 at 3:26
  • @KristjanKica still does not work Commented Nov 4, 2019 at 3:28
  • Looks like on server side you are using JavaScriptSerializer, but you should use JsonSerializer. What kind of application is on server side? ASP.NET, WCF or else? Commented Nov 4, 2019 at 3:35
  • WCF at my Server side. Commented Nov 4, 2019 at 3:45

2 Answers 2

1

Use this extension

public class DateTimeConverter : IsoDateTimeConverter
{
    public DateTimeConverter()
    {
        base.DateTimeFormat = "yyyy-MM-dd";
    }
}

And add to your model

[JsonConverter(typeof(DateTimeConverter))]
public DateTime BirthDate { get; set; }

This way you can send the date in "yyyy-MM-dd" format. You can change it to another format if you like.

Sign up to request clarification or add additional context in comments.

2 Comments

Will have no effect if other than JsonSerializer are used for deserialisation request data. For JsonSerializer given data format is standard, so no need for explicit converter.
I used the code provided but still got the same exception.
1

I used the same json and created in a namespace two classes:

public partial class JsonModel
{
    [JsonProperty("patient")]
    public Patient Patient { get; set; }
}

public partial class Patient
{

    [JsonProperty("Number")]
    public string Number;

    [JsonProperty("FirstName")]
    public string FirstName;

    [JsonProperty("LastName")]
    public string LastName;

    [JsonProperty("BirthDate")]
    public DateTime BirthDate;

    [JsonProperty("Phone")]
    public string Phone;

    [JsonProperty("Mobile")]
    public string Mobile;

    [JsonProperty("Address")]
    public string Address;

    [JsonProperty("Job")]
    public string Job;

    [JsonProperty("Note")]
    public string Note;

    [JsonProperty("GenderId")]
    public int GenderId;

    // Return a textual representation of the order.
    public override string ToString()
    {
        return "FirstName: " + FirstName +
        "\nLastName: " + LastName +
         "\nBirthDate: " + BirthDate;
    }
}

Created a console application for testing purpose.

static void Main(string[] args)
{
    string json = @"{
     ""patient"": {
     ""Number"": 20012,
     ""FirstName"":  ""Anas"",
     ""LastName"":  ""Tina"",
     ""BirthDate"": ""1986-12-29"",
     ""Phone"":  ""000000"",
     ""Mobile"":  ""00000"",
     ""Address"":  ""Damas"",
     ""Job"":  ""Developer"",
     ""Note"":  ""This is a note"",
     ""GenderId"": 1
      }
    } ";

    var test = FromJson(json);
    Console.WriteLine(test.Patient);
    Console.ReadKey();
}

public static JsonModel FromJson(string json)
{
    // Return the result.enter code here
    return JsonConvert.DeserializeObject<JsonModel>(json);
}

Finally the result was:

FirstName: Anas
LastName: Tina
BirthDate: 12/29/1986 12:00:00 AM

3 Comments

Why should I use JsonProperty if I am using DataMember attribute, please?
I updated the question to include my Entity class, please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.