3

I'm trying to set a custom date format when using Newtonsoft.Json ToObject to convert from JSON to my custom Object but trying different ways I can't get it to alter the date format at all.

public class CustomObject
        {
            public string DateRecordedString { get; set; }
        }

static void Main(string[] args)
        {
            string jsonResult = "[{\"daterecordedstring\":\"2016-11-21T08:24:42\"}]";
            JToken jtokenResult = JToken.Parse(jsonResult);

            var objectResult1 = jtokenResult.ToObject<CustomObject[]>();
            Console.WriteLine(objectResult1.First().DateRecordedString); // -> 11/21/2016 08:24:42

            var objectResult2 = jtokenResult.ToObject<CustomObject[]>(new JsonSerializer { DateParseHandling = DateParseHandling.None });
            Console.WriteLine(objectResult2.First().DateRecordedString); // -> 11/21/2016 08:24:42

            var objectResult3 = jtokenResult.ToObject<CustomObject[]>(new JsonSerializer { DateFormatString = "yyyy-M-d" });
            Console.WriteLine(objectResult3.First().DateRecordedString); // -> 11/21/2016 08:24:42

        }

How can I get ToObject to return a date in the format I want (or leave it unchanged)?

6
  • Have you read this newtonsoft.com/json/help/html/datesinjson.htm? Commented Nov 20, 2016 at 21:35
  • Yes thanks, but it hasn't helped - for whatever reason my code above is always producing m/d/y type format. Commented Nov 20, 2016 at 21:38
  • What "culture" is your environment in? Commented Nov 20, 2016 at 21:41
  • New Zealand and m/d/y isn't my date format Commented Nov 20, 2016 at 21:42
  • 1
    You're right it was doing the conversion earlier than I thought. Commented Nov 20, 2016 at 22:04

2 Answers 2

2

JToken's ToObject doesn't respect DateParseHandling=DateParseHandling.None

The code as below: item=>JToken type

Dictionary<string, string> data = item.ToObject<Dictionary<string, string>>(new JsonSerializer { DateParseHandling=DateParseHandling.None});

No work, it still converts to MM/dd/yyyy.

In my case, I get JArray from deserialized string, remember each item in JArray is JToken.

To make it work, add none format instruction before it generates JToken.

So, add DateParseHandling=DateParseHandling.None on deserialize level before it generate JArray.

Newtonsoft.Json.Linq.JArray arr = JsonConvert.DeserializeObject(data, new JsonSerializerSettings {  DateParseHandling=DateParseHandling.None}) as Newtonsoft.Json.Linq.JArray;

Now, if you loop through this JArray for each JToken and use JToken.ToObject, you will get none formatted datetime.

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

Comments

1

Seems like JToken.Parse is doing some type conversion before you do.

As answered in Do not convert JToken date time string as DateTime

void Main()
{
     string jsonResult = "[{\"daterecordedstring\":\"2016-11-21T08:24:42\"}]";

    using (var sr = new StringReader(jsonResult))
    using (var jr = new JsonTextReader(sr) { DateParseHandling = DateParseHandling.None })
    {
        var j = JToken.ReadFrom(jr);
        Console.WriteLine(j["value"].ToString()); // prints '2016-11-21T08:24:42'
    }
}

full kudos to user5090812

Comments

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.