<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>JSON &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/json/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Mon, 07 Feb 2011 12:01:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
<site xmlns="com-wordpress:feed-additions:1">193486638</site>	<item>
		<title>Derserializing ASP.NET MVC JSON formatted Date strings</title>
		<link>/blog/index.php/archives/1183</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Sun, 06 Feb 2011 22:44:47 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MVC]]></category>
		<guid isPermaLink="false">/blog/index.php/archives/1183</guid>

					<description><![CDATA[Have you called an Ajax based action in MVC, only to find that the Date format is being serialized into something that isn’t entirely useful on the client? {&#8220;result&#8221;:[{&#8220;Author&#8221;:&#8221;Aaron&#8221;,&#8221;SentAt&#8221;:&#8221;\/Date(1296824894700)\/&#8221;,&#8221;Text&#8221;:&#8221;Blah Blah&#8221;,&#8221;Id&#8221;:1}],&#8221;runAt&#8221;:&#8221;\/Date(1297031297600)\/&#8221;} You’ll see that the Date is: \/Date(1296824894700)\/ Alas, that’s not very friendly. For a quick translation, I added the following to jQuery’s dataFilter property so [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Have you called an Ajax based action in MVC, only to find that the Date format is being serialized into something that isn’t entirely useful on the client?</p>
<p><strong>{&#8220;result&#8221;:[{&#8220;Author&#8221;:&#8221;Aaron&#8221;,&#8221;SentAt&#8221;:&#8221;\/Date(1296824894700)\/&#8221;,&#8221;Text&#8221;:&#8221;Blah Blah&#8221;,&#8221;Id&#8221;:1}],&#8221;runAt&#8221;:&#8221;\/Date(1297031297600)\/&#8221;}</strong></p>
<p>You’ll see that the Date is:</p>
<p><strong>\/Date(1296824894700)\/</strong></p>
<p>Alas, that’s not very friendly. For a quick translation, I added the following to jQuery’s <a href="http://api.jquery.com/jQuery.ajax/"><strong>dataFilter</strong></a> property so that the date would be formatted in a more human readable format. The <strong>dataFilter</strong> function is used to sanitize a response from a web server.&#160; Here’s what I added:</p>
<pre class="code">$(<span style="color: blue">function </span>() {
    $.ajaxSettings.dataFilter = <span style="color: blue">function </span>(data, type) {
        <span style="color: blue">if </span>(type === <span style="color: maroon">'json'</span>) {
            <span style="color: #006400">// convert things that look like Dates into a UTC Date string
            // and completely replace them. 
            </span>data = data.replace(/(.*?")(\\\/Date\([0-9\-]+\)\\\/)(")/g,
                <span style="color: blue">function </span>(fullMatch, $1, $2, $3) {
                    <span style="color: blue">try </span>{
                        <span style="color: blue">return </span>$1 + <span style="color: blue">new </span>Date(parseInt($2.substr(7))).toUTCString() + $3;
                    }
                    <span style="color: blue">catch </span>(e) {}
                    <span style="color: #006400">// something miserable happened, just return the original string            
                    </span><span style="color: blue">return </span>$1 + $2 + $3;
                });
        }
        <span style="color: blue">return </span>data;
    };
});</pre>
<p>The trick for converting the /\Date(#)\/ syntax easily was from <a href="http://stackoverflow.com/questions/206384/how-to-format-json-date">StackOverflow</a>. It even handles time zone info.</p>
<p>I added a regular expression to look for the various elements that represent a Date in Microsoft JSON serialization format. The resulting date is reformatted to a human readable string. There are 3 capturing groups that I’ve named $1, $2, and $3 which need to be preserved otherwise the JSON string is mangled beyond recognition. Those are returned concatenated to preserve the original formatting of the string.</p>
<p>The resulting date is translated to something like: </p>
<p><strong>Fri, 4 Feb 2011 13:08:14 UTC</strong></p>
<p>Of course, you could do whatever formatting you’d like.</p>
<p>&#160;</p>
<h3>Bonus</h3>
<p>And, as a bonus, you did notice that if you’re using IE9, that the developer tools now have a network trace? Cool. (I’ll still use fiddler for many things, but this will be very handy on machines that don’t have fiddler installed).</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: ; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTML51b4c85a" border="0" alt="SNAGHTML51b4c85a" src="blog/wpcontent/uploads/2011/02/SNAGHTML51b4c85a.png" width="450" height="356" /></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1183</post-id>	</item>
	</channel>
</rss>
