sdfsadfadsf

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Runtime.Serialization.Json;
 7 
 8 namespace Esint.Common
 9 {
10     /// <summary>
11     /// JSON序列化和反序列化辅助类
12     /// </summary>
13     public class JsonHelper
14     {
15         /// <summary>
16         /// JSON序列化
17         /// </summary>
18         public static string JsonSerializer<T>(T t)
19         {
20             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
21             MemoryStream ms = new MemoryStream();
22             ser.WriteObject(ms, t);
23             string jsonString = Encoding.UTF8.GetString(ms.ToArray());
24             ms.Close();
25             return jsonString;
26         }
27 
28         /// <summary>
29         /// JSON反序列化
30         /// </summary>
31         public static T JsonDeserialize<T>(string jsonString)
32         {
33             DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
34             MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
35             T obj = (T)ser.ReadObject(ms);
36             return obj;
37         }
38     }
39 }
posted @ 2012-09-17 14:21  刘伟通  阅读(176)  评论(0)    收藏  举报