Active1 month ago
PM> Install-Package Newtonsoft.Json or Install via VS Package Management. World-class JSON Serializer Serialize and deserialize any.NET object with Json.NET's powerful JSON serializer. LINQ to JSON Create, parse, query and modify JSON using Json.NET's JObject, JArray and JValue objects. Serializing Dates in JSON. With no standard for dates in JSON, the number of possible different formats when interoping with other systems is endless. Converts the to its JSON string representation. Namespace: Newtonsoft.Json Assembly: Newtonsoft.Json (in Newtonsoft.Json.dll) Version: 12.0.1. I'm trying to deserialize/serialize a timespan, but when the json is send it's set to 00:00:00 is this even possible to do? -Kevin Btw, Language is C#, using Visual Studio 2008. The format for TimeSpan used by WCF is not the same one used by the Newtonsoft JSON serializer (JSON.NET). You can serialize one TimeSpan instance using the DataContractJsonSerializer (DCJS), and that will be the format required by a WCF REST service. The code below will print out the formats of some TimeSpan values as serialized by both JSON.NET and DCJS.
I'm trying to deserialize/serialize a timespan,
but when the json is send it's set to 00:00:00is this even possible to do?
Konrad Viltersten![Newtonsoft Json Serialize Timespan Newtonsoft Json Serialize Timespan](https://i.stack.imgur.com/aNrHO.png)
13.5k3535 gold badges152152 silver badges278278 bronze badges
KevinKevin2,79744 gold badges2121 silver badges4343 bronze badges
4 Answers
I tried #Jessycormier's method and it didn't work for me.I ran DataContractJsonSerializer to see what it would generate and I found that gave me a value that looked more like this.
The value shown above was for 1 day, 2 hours, 3 minutes, and 4 seconds.
So it looks like format is:
[-]P[{days}D][T[{hours}H][{min}M][{sec}S]]
Where:
- Indicates negative timespan, omitted for positive values
P must be the first character (unless negative time value)
T must precede the time portion of the timespan.
[] = optional part that may be omitted if 0.
RonnBlackRonnBlackP must be the first character (unless negative time value)
T must precede the time portion of the timespan.
[] = optional part that may be omitted if 0.
2,82033 gold badges2121 silver badges1919 bronze badges
I figured it out, Apparently it's a MS design flaw...
Since TimeSpan cannot be a parameterless object. XML cannot recreate it.
Take a look at this website.http://forums.silverlight.net/forums/p/51793/135450.aspx
Serialize Json C# Newtonsoft
So. Therefore TimeSpan cannot be converted. An easy way to do this isto change the timespan into a string, and then send the string over.and use TimeSpan.TryParse(String);
KevinKevin2,79744 gold badges2121 silver badges4343 bronze badges
These answers are all outdated, so I thought I would provide an updated better answer. moment.js now directly supports .NET
Timespan
serialization format.As of version 2.1.0, this is supported:
John KasterJohn Kaster
If you apply the exact format you can use a TimeSpan. The format is: '0.00:00:00.0000'
Setting a TimeSpan to 30 minutes
This solution works for me. I'm using MVC 4.0 with .Net framework 4.0.
JessycormierJessycormier