![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Collections.Generic;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Linq;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Net;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Controls;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Documents;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Input;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Media;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Media.Animation;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.Windows.Shapes;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
using System.IO;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
namespace Silverlight20.Communication
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
public partial class Json : UserControl
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
public Json()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
InitializeComponent();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 演示如何处理 JSON(对象)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
JsonDemo();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 演示如何处理 JSON(集合)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
JsonDemo2();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// <summary>
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// 演示如何处理 JSON(对象)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// </summary>
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
void JsonDemo()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// REST 服务的 URL
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
Uri uri = new Uri( "http://localhost:3036/REST.svc/User/webabcd/json", UriKind.Absolute);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 实例化 WebClient
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Net.WebClient client = new System.Net.WebClient();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json_DownloadStringCompleted);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
client.DownloadStringAsync(uri);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson.Text = "读取 JSON(对象) 数据中。。。";
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
void json_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
if (e.Error != null)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 发生错误的话,则打印出来
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson.Text = e.Error.ToString();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
return;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 将获得的字符串转换为 JSON(对象)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var ms = new MemoryStream(buffer);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var jsonObject = System.Json.JsonObject.Load(ms) as System.Json.JsonObject;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson.Text = e.Result + "\r\n";
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 解析 JSON(对象)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson.Text += string.Format("姓名: {0}, 生日: {1}",
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
(string)jsonObject["Name"],
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
((DateTime)jsonObject["DayOfBirth"]).ToString("yyyy-MM-dd"));
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/*
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* 总结:
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonObject - 一个具有零或多个 key-value 对的无序集合。继承自抽象类 JsonValue
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonObject.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonObject[key] - JsonObject 索引器,获取 JsonObject 的指定key的value
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonObject.ContainsKey(key) - JsonObject 对象内是否具有指定的key
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
*/
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// <summary>
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// 演示如何处理 JSON(集合)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/// </summary>
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
void JsonDemo2()
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// REST 服务的 URL
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
Uri uri = new Uri("http://localhost:3036/REST.svc/Users/json", UriKind.Absolute);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 实例化 WebClient
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
System.Net.WebClient client = new System.Net.WebClient();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(json2_DownloadStringCompleted);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
client.DownloadStringAsync(uri);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson2.Text = "读取 JSON(集合) 数据中。。。";
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
void json2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
if (e.Error != null)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
{
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 发生错误的话,则打印出来
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson2.Text = e.Error.ToString();
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
return;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
// 将获得的字符串转换为 JSON(集合)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var buffer = System.Text.Encoding.UTF8.GetBytes(e.Result);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var ms = new MemoryStream(buffer);
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
var jsonArray = System.Json.JsonArray.Load(ms) as System.Json.JsonArray;
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson2.Text = e.Result + "\r\n";
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
txtMsgJson2.Text += string.Format("姓名: {0}, 生日: {1}",
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
(string)jsonArray.First()["Name"],
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
((DateTime)jsonArray.Single(p => p["Name"] == "webabcd02")["DayOfBirth"]).ToString("yyyy-MM-dd"));
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
/*
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* 总结:
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonArray - 一个具有零或多个 JsonValue(抽象类,JsonObject和JsonArray都继承自此类) 对象的有序序列
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonArray.Load(Stream) - 将指定的字符串流反序列化为 JSON 对象(CLR可用的类型)
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonArray[index] - JsonArray 索引器,获取 JsonArray 的指定索引的 JsonValue
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
* JsonArray 支持 LINQ
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
*/
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}
![](http://img.e-com-net.com/image/info3/78a0868db15f4e33ae8821aa4127a24d.gif)
}