0

In a C# project, I'm retrieving an Event object, with subject (string), location (string), start_time (datetime), end_time (datetime), all_day_event (boolean) and calendar_id (int) properties, as a JSON response from a Rest request. That request gets data from a database and start_time and end_time columns' types are datetime on that database too. But I'm having trouble about getting the datetime type values. I get the JSON object like this:

[
  {
    "id": 1,
    "subject": "Test Event 1",
    "location": "Test Location",
    "start_time": "2019-08-22 10:17:53",
    "end_time": "2019-08-22 10:17:55",
    "all_day_event": 0,
    "calendar_id": 1
  }
]

This is my Event class:

public class Event {

        public int Id { get; set; }

        public string Subject { get; set; }

        public string Location { get; set; }

        public DateTime StartTime { get; set; }

        public DateTime EndTime { get; set; }

        public bool AllDayEvent { get; set; }

        public int CalendarId { get; set; }
    }

And this is how I deserialize the JSON object:

events = JsonConvert.DeserializeObject<Event[]>(response.Content);

When I print that JSON object directly, it is displayed correctly. But after I deserialize it, the time values are displayed as 01/01/0001 12:00 AM. I've searched for a while. I've found Deserialize Json Object - DateTime, Deserializing Import Io JSON with multiple objects, Deserialize multiple json into object c# suggestions but they didn't help. So is there any way to get those time values properly?? Thank you so much.

1
  • 2
    You need to apply [JsonProperty("start_time")] attribute to StartTime property Commented Aug 22, 2019 at 11:35

1 Answer 1

1

As Aleks said, you need to decorate properties which have different naming convention. Keep in mind, Json.Net will automatically map JSON's snakeCase into C#'s PascalCase properties. However, if you have snake_case or something else, you need to decorate properties.

public class Event 
{
        public int Id { get; set; }

        public string Subject { get; set; }

        public string Location { get; set; }

        [JsonProperty("start_time")]
        public DateTime StartTime { get; set; }

        [JsonProperty("end_time")]
        public DateTime EndTime { get; set; }

        [JsonProperty("all_day_event")]
        public bool AllDayEvent { get; set; }

        [JsonProperty("calendar_id")]
        public int CalendarId { get; set; }
    }

In addition, you can set global naming strategy, to avoid decorating all the properties of your model, if JSON follows snake_case for example

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

1 Comment

@OğuzÖztınaz check documentation example, maybe it will be better for you to use SnakeCaseNamingStrategy instead of decorating all the models you (might) have

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.