Overview
The release of Silverlight 2 Beta 1 brings us a lot of surprises from Runtime and Tools, such as supporting the framework languages Visual Basic, Visual C #, IronRuby, Ironpython, A series of new features such as JSON, Web Service, WCF, and Sockets support. The one-step learning Silverlight 2 series article takes you to Silverlight 2 development quickly.
This article briefly introduces the support for JSON in Silverlight 2.
Simple Example
In this article, we still use the example (to put it apart) that we used in the previous two articles to show the latest essay. The final result is shown in:
First, we create a server to provide data in JSON format. To generate data in JSON format, we use Json. NET, an open-source project. Create two object types:
public class Post{ public int Id { get; set; } public string Title { get; set; } public string Author { get; set; }}
public class Blog{ public List<Post> Posts { get; set; }}
In the Silverlight project, we will also use these two entity classes to create a new HttpHandler and generate data in JSON format. We use Json.. NET. the SerializeObject method serializes an object in JSON format:
Public class BlogHandler: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/plain"; List <Post> posts = new List <Post> () {new Post {Id = 1, title = "one-step Silverlight 2 series (13): WebRequest for data and communication", Author = "TerryLee"}, new Post {Id = 2, title = "one-step Silverlight 2 series (12): WebClient for data and communication", Author = "TerryLee"}, new Post {Id = 3, title = "one-step Silverlight 2 series (11): Data Binding", Author = "TerryLee"}, new Post {Id = 4, title = "one-step Silverlight 2 series (10): using user controls", Author = "TerryLee"}, new Post {Id = 5, title = "one-step Silverlight 2 series (9): using the control template", Author = "TerryLee"}, new Post {Id = 6, title = "one-step Silverlight 2 series (8): style encapsulation controls", Author = "TerryLee" }}; Blog blog = new Blog (); blog. posts = posts; context. response. write (JavaScriptConvert. serializeObject (blog);} public bool IsReusable {get {return false ;}}}
Now test HttpHandler and check the generated data format:
It looks more obvious to format the data. Here we recommend an online JSON data formatting tool http://www.curiousconcept.com/jsonformatter /:
The formatted data is as follows:
Now, JSON data is obtained in Silverlight and deserialized, and the interface layout XAML is no longer pasted, just like the previous two examples. In Silverlight 2, built-in support for JSON is provided through the namespace System. Runtime. Serialization. Json, which is located in System. ServiceModel. Web. dll.
We use WebRequest to obtain data:
private void UserControl_Loaded(object sender, RoutedEventArgs e){ Uri endpoint = new Uri("http://localhost:8081/BlogHandler.ashx"); WebRequest request = WebRequest.Create(endpoint); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.BeginGetResponse(new AsyncCallback(ResponseReady), request);}void ResponseReady(IAsyncResult asyncResult){ WebRequest request = asyncResult.AsyncState as WebRequest; WebResponse response = request.EndGetResponse(asyncResult); using (Stream responseStream = response.GetResponseStream()) { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Blog)); Blog blog = jsonSerializer.ReadObject(responseStream) as Blog; Posts.ItemsSource = blog.Posts; }}
DataContractJsonSerializer is used to serialize an object to JSON or deserialize it into an object instance. The WriteObject and ReadObject methods are used respectively.
So far, a complete example of JSON support in Silverlight 2 is complete. The effect after running is the same as that in the previous example:
Conclusion
This article briefly introduces the support for JSON in Silverlight 2. DataContractJsonSerializer is used to serialize an object to JSON or deserialize it into an object instance. You can download the sample code in this article from here.
Next article: Step by Step Silverlight 2 series (17): ADO. NET Data Services for Data and communication