Consume a JSON object in Silverlight
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# classes to mirror the JSON object. It’s important to make sure the variables in the class are exactly how they appear in the JSON object, this is case sensitive.

- Before adding the deserializing code, add references in your project for System.Runtime.Serialization, System.ServiceModel, and System.ServiceModel.Web.

- 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 System.Runtime.Serialization.Json.DataContractJsonSerializer.

- 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’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
function sendJSON(){ document.getElementById("SL").Content.Bridge.SendJSON(Object.toJSON(Person)); }
HTML
<input type="button" value="send JSON to SL" onclick="sendJSON()" id="btnSendJSON" />
[...] Consume a JSON Object in Silverlight (Corey Schuman) [...]