<?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/"
	>

<channel>
	<title>Corey Schuman &#187; DataContractJsonSerializer</title>
	<atom:link href="http://www.85turns.com/tag/datacontractjsonserializer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.85turns.com</link>
	<description>Silverlight Consultant</description>
	<lastBuildDate>Tue, 26 Apr 2011 20:57:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Consume a JSON object in Silverlight</title>
		<link>http://www.85turns.com/2008/03/12/consume-a-json-object-in-silverlight/</link>
		<comments>http://www.85turns.com/2008/03/12/consume-a-json-object-in-silverlight/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 02:49:50 +0000</pubDate>
		<dc:creator>Corey</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DataContractJsonSerializer]]></category>
		<category><![CDATA[deserialize]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://simplesilverlight.wordpress.com/?p=25</guid>
		<description><![CDATA[Here is how to consume a JSON object in Silverlight 2. Start with a JSON object: var Person = { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 } } Create an entry point into Silverlight. You will need to add namespace System.Windows.Browser. Next add C# [...]]]></description>
			<content:encoded><![CDATA[<p>Here is how to consume a JSON object in Silverlight 2.</p>
<ol>
<li>Start with a JSON object:<br />
<code>var Person = {<br />
"firstName": "John",<br />
"lastName": "Smith",<br />
"address": {<br />
"streetAddress": "21 2nd Street",<br />
"city": "New York",<br />
"state": "NY",<br />
"postalCode": 10021<br />
}<br />
}</code></li>
<li>Create an entry point into Silverlight.  You will need to add namespace <b>System.Windows.Browser</b>.<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/json_htmlbridge.png" alt="JSON Html Bridge" /></li>
<li>Next add C# classes to mirror the JSON object.  It&#8217;s important to make sure the variables in the class are exactly how they appear in the JSON object, this is case sensitive.<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/json_csclasses.png" alt="JSON C# classes" /></li>
<li>Before adding the deserializing code, add references in your project for <b>System.Runtime.Serialization</b>, <b>System.ServiceModel</b>, and <b>System.ServiceModel.Web</b>.<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/json_addreferences.png" alt="Add reference" /></li>
<li>Now that the proper references are made in the project, we can finally write the code that deserializes the JSON object.  The class that does all the magic is <b>System.Runtime.Serialization.Json.DataContractJsonSerializer</b>.<br />
<img src="http://simplesilverlight.files.wordpress.com/2008/03/json_desrializer.png" alt="Code for deserialzing a json object in silverlight" /></li>
<li>Finally add some HTML and JavaScript to make this work. (To get the JSON object in a serialized form I am using the Prototype JavaScript library.  That is where you&#8217;ll find the function Object.toJSON().  There are other ways to serialize a JSON object.  Notably, if you are using the AJAX ASP.NET library, there is a function in there that does exactly the same thing as this call.)JavaScript<br />
<code>function sendJSON(){ document.getElementById("SL").Content.Bridge.SendJSON(Object.toJSON(Person)); }</code><br />
HTML<br />
<code>&lt;input type="button" value="send JSON to SL" onclick="sendJSON()" id="btnSendJSON" /&gt;</code></li>
</ol>
<p><b>Links</b><br />
<a href="http://www.peakeeng.com/Silverlight/JsonToSilverlight/index.html" title="http://www.peakeeng.com/Silverlight/JsonToSilverlight/index.html"> See this example in action</a>.<br />
<a href="http://silverlight.peterpancomplex.net/zip/JsonToSilverlight.zip" title="http://silverlight.peterpancomplex.net/zip/JsonToSilverlight.zip"> Download the Source</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.85turns.com/2008/03/12/consume-a-json-object-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

