Silverlight 使用DataContractJsonSerializer序列化与反序列化 Json

环境说明:Silverlight 5.1,.Net Framework  ​4.0

1.添加引用System.ServiceModel.Web.dll。

  因为 System.Runtime.Serialization.Json.DataContractJsonSerializer 类的引用是在System.ServiceModel.Web.dll这个程序集中。

2.代码如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Windows;
 6 using System.Windows.Controls;
 7 using System.Windows.Documents;
 8 using System.Windows.Input;
 9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using System.IO;
13 using System.Text;
14 using System.Runtime.Serialization.Json;
15 
16 namespace SilverlightApplication2
17 {
18     public partial class MainPage : UserControl
19     {
20         public MainPage()
21         {
22             InitializeComponent();
23         }
24 
25         private void button1_Click(object sender, RoutedEventArgs e)
26         {
27             //textBox1.Text = "test button";
28 
29             List<TestJson> list = new List<TestJson>(){
30             new TestJson(){ Id=1,Name="test1"},
31             new TestJson(){ Id=2,Name="test2"},
32             new TestJson(){ Id=3,Name="test3"},
33             new TestJson(){ Id=4,Name="test4"}
34             };
35 
36             string json = JsonHelp.JsonSerializer<List<TestJson>>(list);            
37             MessageBox.Show(json);
38             
39             var result = JsonHelp.JsonDeSerializer<List<TestJson>>(json);
40             var resultJson = result.FirstOrDefault();
41             MessageBox.Show(string.Format("id={0},name={1}", resultJson.Id, resultJson.Name));
42         }
43     }
44 
45     public class TestJson
46     {
47         public int Id { get; set; }
48         public string Name { get; set; }
49     }
50 
51     public class JsonHelp
52     {
53         /// <summary>
54         /// json序列化
55         /// </summary>
56         /// <typeparam name="T"></typeparam>
57         /// <param name="t"></param>
58         /// <returns></returns>
59         public static string JsonSerializer<T>(T t) where T : class
60         {
61             DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(T));
62             using (MemoryStream ms = new MemoryStream())
63             {
64                 dcs.WriteObject(ms, t);
65                 byte[] bs = ms.ToArray();
66                 string jsonStr = Encoding.UTF8.GetString(bs, 0, bs.Length);
67                 return jsonStr;
68             }
69         }
70 
71         /// <summary>
72         /// json反序列化
73         /// </summary>
74         /// <typeparam name="T"></typeparam>
75         /// <param name="jsonStr"></param>
76         /// <returns></returns>
77         public static T JsonDeSerializer<T>(string jsonStr) where T : class
78         {
79             DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(T));
80             using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
81             {
82                 return dcs.ReadObject(ms) as T;
83             }
84         }
85     }
86 }

 

3.序列化结果

4.反序列化结果

posted @ 2016-11-24 16:26  虹色流星雨  阅读(245)  评论(0编辑  收藏  举报