木子冬
程序就是规范流程下的替代品,互联网就是对应需求下的一个产业链!

导航

 
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Runtime.Serialization.Json;
 6 using System.IO;
 7 using System.Text;
 8 
 9 /// JSON序列化和反序列化辅助类
10 /// </summary>
11 public class JsonHelper
12 {
13     /// <summary>
14     /// JSON序列化
15     /// </summary>
16     public static string JsonSerializer<T>(T t)
17     {
18         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
19         MemoryStream ms = new MemoryStream();
20         ser.WriteObject(ms, t);
21         string jsonString = Encoding.UTF8.GetString(ms.ToArray());
22         ms.Close();
23         return jsonString;
24     }
25 
26     /// <summary>
27     /// JSON反序列化
28     /// </summary>
29     public static T JsonDeserialize<T>(string jsonString)
30     {
31         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
32         MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
33         T obj = (T)ser.ReadObject(ms);
34         return obj;
35     }
36 }
View Code

感谢:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html

posted on 2014-11-28 17:07  木子_冬  阅读(209)  评论(0编辑  收藏  举报