C#中JsonHelper详细代码-用于序列化和反序列化

时间:2022-04-12 20:53:36
C#中JsonHelper详细代码-用于序列化和反序列化C#中JsonHelper详细代码-用于序列化和反序列化
 1 /// <summary>
 2         /// 转换对象为JSON格式数据
 3         /// </summary>
 4         /// <typeparam name="T"></typeparam>
 5         /// <param name="obj">对象</param>
 6         /// <returns>字符格式的JSON数据</returns>
 7         public string GetJSON<T>(object obj)
 8         {
 9             string result = String.Empty;
10             try
11             {
12                 System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
13                 new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
14                 using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
15                 {
16                     serializer.WriteObject(ms, obj);
17                     result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
18                 }
19             }
20             catch (Exception ex)
21             {
22                 throw ex;
23             }
24             return result;
25         }
26         /// <summary>
27         /// 转换List<T>的数据为JSON格式
28         /// </summary>
29         /// <typeparam name="T"></typeparam>
30         /// <param name="vals">列表值</param>
31         /// <returns>JSON格式数据</returns>
32         public string JSON<T>(List<T> vals)
33         {
34             System.Text.StringBuilder st = new System.Text.StringBuilder();
35             try
36             {
37                 System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
38 
39                 foreach (T city in vals)
40                 {
41                     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
42                     {
43                         s.WriteObject(ms, city);
44                         st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
45                     }
46                 }
47             }
48             catch (Exception ex)
49             {
50                 throw ex;
51             }
52 
53             return st.ToString();
54         }
55         /// <summary>
56         /// JSON格式字符转换为T类型的对象
57         /// </summary>
58         /// <typeparam name="T"></typeparam>
59         /// <param name="jsonStr"></param>
60         /// <returns></returns>
61         public T ParseFormByJson<T>(string jsonStr)
62         {
63             T obj = Activator.CreateInstance<T>();
64             using (System.IO.MemoryStream ms =
65             new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonStr)))
66             {
67                 System.Runtime.Serialization.Json.DataContractJsonSerializer serializer =
68                 new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(T));
69                 return (T)serializer.ReadObject(ms);
70             }
71         }
72 
73         public string JSON1<SendData>(List<SendData> vals)
74         {
75             System.Text.StringBuilder st = new System.Text.StringBuilder();
76             try
77             {
78                 System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SendData));
79 
80                 foreach (SendData city in vals)
81                 {
82                     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
83                     {
84                         s.WriteObject(ms, city);
85                         st.Append(System.Text.Encoding.UTF8.GetString(ms.ToArray()));
86                     }
87                 }
88             }
89             catch (Exception ex)
90             {
91                 throw ex;
92             }
93 
94             return st.ToString();
95         }    
View Code

如果提示不存在Json命名空间,在引用中添加一下引用:

System.Runtime.Serialization