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; }
}
Anas Tina
  • 329
  • 1
  • 2
  • 14
  • Can you provide the code that generates JSON? – Ray Krungkaew Nov 04 '19 at 03:23
  • Try as timestamp /Date(1379048144000)/ – kkica Nov 04 '19 at 03:26
  • @KristjanKica still does not work – Anas Tina Nov 04 '19 at 03: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? – Fabio Nov 04 '19 at 03:35
  • WCF at my Server side. – Anas Tina Nov 04 '19 at 03:45
  • [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "JSON/Patient/Add")] ResultClass AddPatient(Patient patient); – Anas Tina Nov 04 '19 at 03:48
  • Does this answer your question? [Content '\/Date('')\/' does not start with '\/Date(' and end with ')\/' as required for JSON](https://stackoverflow.com/questions/44227653/content-date-does-not-start-with-date-and-end-with-as-requi) – Divyang Desai Nov 04 '19 at 05:17
  • No, @Div I already examined the solution in the mentioned question. – Anas Tina Nov 04 '19 at 05:20
  • @AnasTina Have you tried using `Newtonsoft JSON` package: https://www.newtonsoft.com/json ? In WCF, you can replace your `DataContractJsonSerializer` with this package: https://stackoverflow.com/questions/11153628/replace-wcf-built-in-javascriptserializer-with-newtonsoft-json-net-json-serializ – Rahul Sharma Nov 04 '19 at 07:00

2 Answers2

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.

kkica
  • 4,034
  • 1
  • 20
  • 40
  • 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. – Fabio Nov 04 '19 at 03:37
  • I used the code provided but still got the same exception. – Anas Tina Nov 04 '19 at 03:46
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
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76