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 }
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 }
