Json parse date time format

I'm using Newtonsoft.Json to parse json objects but one object that have date and time in it wont be parsed as it's stated.

JObject a = JObject.Parse(response);

a will get the whole json and last is the object i want.

, "last_activity_date": "2017-03-29T18:05:38.707Z"}

var date = a["last_activity_date"];

will output

date = 2017-03-29 18:05:38

Is it possible to keep the date time as it is? 2017-03-29T18:05:38.707Z Or do i need to use regex?

Upvotes: 2

Views: 476

Answers (1)

Darjan Bogdan
Darjan Bogdan

Reputation: 3900

Due to default JObject parsing configuration, your last_activity_date will be treated as Date type, if you want to treat it as a string, you'll need to create dedicated class and deserialize your JSON into object.

public class Root
{
    [JsonProperty("last_activity_date")]
    public string LastActivityDate { get; set; }
}

You can use JsonConvert:

var obj = (Root)JsonConvert.DeserializeObject(json, typeof(Root));
Console.WriteLine(obj.LastActivityDate); //outputs: 2017-03-29T18:05:38.707Z

Upvotes: 1

Related Questions