LINQPad query to parse JSON DateTime string

JSON (JavaScript Object Nota­tion) is a data trans­fer for­mat wide­ly adopt­ed on the web because it’s light­weight com­pared to XML. I touched on the top­ic of JSON in an ear­li­er post when I post­ed some exten­sion meth­ods for compressing/decompressing string.

If you are unfa­mil­iar with the JSON for­mat or how it’s sup­port­ed in .Net then you should take a look at the MSDN arti­cles in the ref­er­ences sec­tion to get you start­ed.

With regards to Date­Time val­ues, they appear as JSON strings in the form of “\/Date(1276675934513+0100)\/” which is not easy to read when you’re debug­ging. In order to con­vert this JSON string back to a human­ly read­able form you can use this LIN­Q­Pad to parse it, just replace the JSON string at the top:

var json = "\"\\/Date(1276675934513+0100)\\/\"";
var serializer = new DataContractJsonSerializer(typeof(DateTime));
var memString = new MemoryStream(Encoding.ASCII.GetBytes(json));
var d = (DateTime) serializer.ReadObject(memString);

If you’re not using LIN­Q­Pad already then you should! Writ­ten by Joe Alba­hari (co-writer of the C# in a Nut­shell books) it can be a huge time saver because you can use it as a code snip­pet edi­tor in addi­tion to being a Data­base query tool.


An intro­duc­tion to JSON in JavaScript and .Net

Stand-alone JSON seri­al­iza­tion

JSON For­mat­ter & Val­ida­tor

JSON.Net Code­plex project


Liked this post? Why not support me on Patreon and help me get rid of the ads!