0

I am trying to parse JSON string into JObject using JObject.Parse().

But its not working ,This is my code:

string json="{\"hashkey\":\"paphAsethE2rexev6c5qAbayu3ebEc\",\"expiration\":\"2016-11-24T12:00:00.000Z\"}";

JObject resourceJson = JObject.Parse(json);
                        return Ok(resourceJson);

but in the output some value of expiration is missing that is(.000)

Expected Result:

   {
      "hashkey": "paphAsethE2rexev6c5qAbayu3ebEc",
      "expiration": "2016-11-24T12:00:00.000Z"
    }

the result which i am getting is:

{
  "hashkey": "paphAsethE2rexev6c5qAbayu3ebEc",
  "expiration": "2016-11-24T12:00:00Z"
}

please help

Sandeep
  • 1,504
  • 7
  • 22
  • 32
  • are you sure JObject lost milliseconds? Did you try to parse more then 0 ms and check value in DateTime? – ad1Dima Feb 18 '16 at 06:20

1 Answers1

1

You can set the DateTime format in WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    //...

    var converter = new Newtonsoft.Json.Converters.IsoDateTimeConverter 
                                     {DateTimeFormat="yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffK"};
    config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(converter);

    // ...
}
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • Tried this its working but the problem is if the string has "2016-11-24T12:00:00Z" even then its parsing it a s"2016-11-24T12:00:00.000Z" – Ashok Padarthi Feb 18 '16 at 09:50
  • When you call `JObject.Parse()`, it loses the information about the original format, so you can't preserve whether milliseconds were specified or not. – Mark Cidade Feb 18 '16 at 09:58