稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据

[索引页]
[源码下载]


稳扎稳打Silverlight(19) - 2.0通信之调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据


作者:webabcd


介绍
Silverlight 2.0 调用REST服务,处理JSON格式, XML格式, RSS/ATOM格式的数据
    通过 System.Net.WebClient 类调用 REST 服务
    通过 System.Json 命名控件下的类处理 JSON 数据
    通过 System.Xml.Linq 命名空间下的类(LINQ to XML)处理 XML 数据
    通过 System.ServiceModel.Syndication 命名空间下的类处理 RSS/ATOM 数据


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779


示例
1、调用 REST 服务,返回 JSON 数据
REST.cs(WCF创建的REST服务)
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;

/// <summary>
/// 提供 REST 服务的类
/// 注:Silverlight只支持 GET 和 POST
/// </summary>
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class REST
{
         /// <summary>
         /// 用于演示返回 JSON(对象) 的 REST 服务
         /// </summary>
         /// <param name="name"></param>
         /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "User/{name}/json", ResponseFormat = WebMessageFormat.Json)]
         public User HelloJson( string name)
        {
                 return new User { Name = name, DayOfBirth = new DateTime(1980, 2, 14) };
        }

         /// <summary>
         /// 用于演示返回 JSON(集合) 的 REST 服务
         /// </summary>
         /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "Users/json", ResponseFormat = WebMessageFormat.Json)]
         public List<User> HelloJson2()
        {
                 return new List<User>    
                {    
                         new User(){ Name = "webabcd01", DayOfBirth = new DateTime(1980, 1, 1) },
                         new User(){ Name = "webabcd02", DayOfBirth = new DateTime(1980, 2, 2) },
                         new User(){ Name = "webabcd03", DayOfBirth = new DateTime(1980, 3, 3) },
                };
        }
}
 
Json.xaml 
<UserControl x:Class="Silverlight20.Communication.Json"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="Left" Width="600">
        
                <TextBox x:Name="txtMsgJson" Margin="5" />
                <TextBox x:Name="txtMsgJson2" Margin="5" />    
                
        </StackPanel>
</UserControl>
 
Json.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.IO;

namespace Silverlight20.Communication
{
         public partial class Json : UserControl
        {
                 public Json()
                {
                        InitializeComponent();

                         // 演示如何处理 JSON(对象)
                        JsonDemo();

                         // 演示如何处理 JSON(集合)
                        JsonDemo2();
                }

                 /// <summary>
                 /// 演示如何处理 JSON(对象)
                 /// </summary>
                 void JsonDemo()
                {
                         // REST 服务的 URL
                        Uri uri = new Uri( "http://localhost:3036/REST.svc/User/webabcd/json", UriKind.Absolute);

                        // 实例化 WebClient
                        System.Net.WebClient client = new System.Net.WebClient();

                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json_DownloadStringCompleted);
                        client.DownloadStringAsync(uri);

                        txtMsgJson.Text = "读取 JSON(对象) 数据中。。。";
                }

                void json_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
                {
                        if (e.Error != null)
                        {
                                // 发生错误的话,则打印出来
                                txtMsgJson.Text = e.Error.ToString();
                                return;
                        }

                        // 将获得的字符串转换为 JSON(对象)
                        var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
                        var ms = new MemoryStream(buffer);
                        var jsonObject = System.Json.JsonObject.Load(ms) as System.Json.JsonObject;

                        txtMsgJson.Text = e.Result + "\r\n";
                        // 解析 JSON(对象)
                        txtMsgJson.Text += string.Format("姓名: {0}, 生日: {1}",
                                (string)jsonObject["Name"],
                                ((DateTime)jsonObject["DayOfBirth"]).ToString("yyyy-MM-dd"));

                        /*    
                         * 总结:
                         * JsonObject - 一个具有零或多个 key-value 对的无序集合。继承自抽象类 JsonValue
                         *         JsonObject.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
                         *         JsonObject[key] - JsonObject 索引器,获取 JsonObject 的指定key的value
                         *         JsonObject.ContainsKey(key) - JsonObject 对象内是否具有指定的key
                         */
                }

                /// <summary>
                /// 演示如何处理 JSON(集合)
                /// </summary>
                void JsonDemo2()
                {
                        // REST 服务的 URL
                        Uri uri = new Uri("http://localhost:3036/REST.svc/Users/json", UriKind.Absolute);

                        // 实例化 WebClient
                        System.Net.WebClient client = new System.Net.WebClient();

                        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json2_DownloadStringCompleted);
                        client.DownloadStringAsync(uri);

                        txtMsgJson2.Text = "读取 JSON(集合) 数据中。。。";
                }

                void json2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
                {
                        if (e.Error != null)
                        {
                                // 发生错误的话,则打印出来
                                txtMsgJson2.Text = e.Error.ToString();
                                return;
                        }

                        // 将获得的字符串转换为 JSON(集合)
                        var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
                        var ms = new MemoryStream(buffer);
                        var jsonArray = System.Json.JsonArray.Load(ms) as System.Json.JsonArray;

                        txtMsgJson2.Text = e.Result + "\r\n";
                        txtMsgJson2.Text += string.Format("姓名: {0}, 生日: {1}",
                                (string)jsonArray.First()["Name"],
                                ((DateTime)jsonArray.Single(p => p["Name"] == "webabcd02")["DayOfBirth"]).ToString("yyyy-MM-dd"));

                        /*    
                         * 总结:
                         * JsonArray - 一个具有零或多个 JsonValue(抽象类,JsonObject和JsonArray都继承自此类) 对象的有序序列
                         *         JsonArray.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
                         *         JsonArray[index] - JsonArray 索引器,获取 JsonArray 的指定索引的 JsonValue
                         *         JsonArray 支持 LINQ
                         */
                }
        }
}
 
未完待续>>
 
 
OK
[源码下载]
 

你可能感兴趣的:(json,xml,rss,REST,ATOM)