C# 结构体和List<T>类型数据转Json数据保存和读取

C#  结构体和List<T>类型数据转Json数据保存和读取

  1 一.结构体转Json
  2 
  3     public struct FaceLibrary     
  4     {
  5         public string face_name;
  6         public byte[] face_Feature;
  7     }
  8 
  9     //序列化结构体
 10     facelibrary = new FaceLibrary();
 11     facelibrary.face_name = "zhangsan";
 12     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 13 
 14     MemoryStream stream1 = new MemoryStream();
 15     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(FaceLibrary));
 16     ser.WriteObject(stream1, facelibrary);
 17     stream1.Position = 0;
 18     StreamReader sr = new StreamReader(stream1);
 19     Console.Write("JSON form of Person object: ");
 20     Console.WriteLine(sr.ReadToEnd());
 21 
 22     //打印结果:
 23         JSON form of Person object: [{"face_Feature":[170,34,54,69,255],"face_name":"zhangsan"}
 24 
 25     //反序列化结构体
 26     stream1.Position = 0;
 27     FaceLibrary p2 = (FaceLibrary)ser.ReadObject(stream1);
 28     Console.WriteLine(p2.face_name);
 29     Console.WriteLine(p2.face_Feature);
 30     //输出结果:
 31         wangjin01
 32         System.Byte[]
 33         
 34     说明: 通过如上操作,能够将结构体的数据转为Json数据    
 35     
 36     
 37 方法二: 将List<T> 结构的数据转为Json数据
 38     
 39     public struct FaceLibrary     
 40     {
 41         public string face_name;
 42         public byte[] face_Feature;
 43     }
 44     
 45     List<FaceLibrary> listfacex = new List<FaceLibrary>();
 46     
 47     //序列化List<T> 
 48     facelibrary = new FaceLibrary();
 49     facelibrary.face_name = "zhangsan01";
 50     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 51     listfacex.Add(facelibrary);
 52 
 53     facelibrary.face_name = "zhangsan02";
 54     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 55     listfacex.Add(facelibrary);
 56 
 57     facelibrary.face_name = "zhangsan03";
 58     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 59     listfacex.Add(facelibrary);
 60 
 61     facelibrary.face_name = "zhangsan04";
 62     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 63     listfacex.Add(facelibrary);
 64 
 65     facelibrary.face_name = "zhangsan05";
 66     facelibrary.face_Feature = new byte[] { 0xaa, 0x22, 0x36, 0x45, 0xff };
 67     listfacex.Add(facelibrary);
 68 
 69     MemoryStream stream1 = new MemoryStream();
 70     DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<FaceLibrary>));
 71     ser.WriteObject(stream1, listfacex);
 72     stream1.Position = 0;
 73     StreamReader sr = new StreamReader(stream1);
 74     Console.Write("JSON form of Person object: ");
 75     Console.WriteLine(sr.ReadToEnd());
 76     
 77     //输出结果:
 78         JSON form of Person object: [
 79             {"face_Feature":[170,34,54,69,255],"face_name":"zhangsan01"},
 80             {"face_Feature":[170,34,54,69,255],"face_name":"zhangsan02"},
 81             {"face_Feature":[170,34,54,69,255],"face_name":"zhangsan03"},
 82             {"face_Feature":[170,34,54,69,255],"face_name":"zhangsan04"},
 83             {"face_Feature":[170,34,54,69,255],"face_name":"zhangsan05"}
 84         ]
 85 
 86     //反序列化List<T> 
 87     stream1.Position = 0;
 88     List<FaceLibrary> p2 = (List<FaceLibrary>)ser.ReadObject(stream1);
 89     Console.WriteLine(p2[0].face_name);
 90     Console.WriteLine(p2[0].face_Feature);
 91     //输出结果:
 92         zhangsan01
 93         System.Byte[]
 94 
 95 
 96 
 97 三.测试应用
 98     /*     
 99     应用需求: 
100         1. 通过List<T>将结构体类型<T>的多个数据加载到List<T>中;
101         2. 将List<T>数据转为Json数据,并写入到文件中;
102         3. 从文件中读取保存的Json数据,并还原成List<T>数据供后续调用;    
103 
104     
105         //说明:  JavaScriptSerializer 需要导入相应的库,
106             命名空间:   System.Web.Script.Serialization
107             程序集:  System.Web.Extensions(位于 System.Web.Extensions.dll)
108             参考文件:https://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx
109     */    
110     
111 
112     //1.定义结构体和List<T>
113         public struct FaceLibrary     
114         {
115             public string face_name;
116             public byte[] face_Feature;
117         }
118         FaceLibrary facelibrary;
119         List<FaceLibrary> listfacex = new List<FaceLibrary>();
120     
121     //2. 添加数据到List<T>中
122         facelibrary.face_name = "zhangsan01";
123         facelibrary.face_Feature = new byte[] { 0xaa, 0x01, 0x11, 0x21, 0xff };
124         listfacex.Add(facelibrary);
125 
126         facelibrary.face_name = "zhangsan02";
127         facelibrary.face_Feature = new byte[] { 0xaa, 0x02, 0x12, 0x22, 0xff };
128         listfacex.Add(facelibrary);
129         
130         facelibrary.face_name = "zhangsan03";
131         facelibrary.face_Feature = new byte[] { 0xaa, 0x03, 0x13, 0x23, 0xff };
132         listfacex.Add(facelibrary);
133 
134         facelibrary.face_name = "zhangsan04";
135         facelibrary.face_Feature = new byte[] { 0xaa, 0x04, 0x14, 0x24, 0xff };
136         listfacex.Add(facelibrary);
137 
138         facelibrary.face_name = "zhangsan05";
139         facelibrary.face_Feature = new byte[] { 0xaa, 0x05, 0x15, 0x25, 0xff };
140         listfacex.Add(facelibrary);
141     
142     //3. 将List<T>数据转为Json数据
143         String str = JsonListToString(listfacex);    
144         //JSON序列化,将List<T>转换为String
145         private String JsonListToString (List<FaceLibrary> list)
146         {
147             JavaScriptSerializer Serializerx = new JavaScriptSerializer();
148             string changestr = Serializerx.Serialize(list);
149             return changestr;
150         }
151 
152     //4. 将Json数据写入文件系统
153         FileWrite("test.txt",str)
154         //写入文件
155         private void FileWrite(string filepath,string writestr)
156         {
157             FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
158             StreamWriter sw = new StreamWriter(fs);
159             sw.Write(writestr);
160             sw.Close();
161             fs.Close();
162         }
163         
164     //5.  从文件中读取Json数据
165     
166         string str =  FileRead("test.txt");
167         //读取文件
168         private string  FileRead(string filepath)
169         {
170             FileStream fs = new FileStream(filepath, FileMode.Open);
171             StreamReader sr = new StreamReader(fs);
172             string str = sr.ReadToEnd();
173             sr.Close();
174             fs.Close();
175             return str;
176         }
177 
178     //6. 将Json数据转换为List<T>数据
179         
180         List<FaceLibrary> listface =   StringToJsonList(str);    
181         //JSON反序列化,将List<T>转换为String
182         private List<FaceLibrary> StringToJsonList(string str)
183         {
184             JavaScriptSerializer Serializer = new JavaScriptSerializer();
185             List<FaceLibrary> face = Serializer.Deserialize<List<FaceLibrary>>(str);
186             return face;
187         }
188 
189 
190 //以上方法基本能够解决上述问题;
191 
192 
193 
194 
195 
196 
197 以上代码基于参考如下代码,
198 /*
199 //    C#将Json字符串反序列化成List对象类集合
200 
201     using System.IO;
202     using System.Web.Script.Serialization;
203     using System.Runtime.Serialization.Json;
204     public static List<T> JSONStringToList<T>(this string JsonStr)
205     {
206 
207         JavaScriptSerializer Serializer = new JavaScriptSerializer();
208 
209         List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
210 
211         return objs;
212 
213     }
214      
215     public static T Deserialize<T>(string json)
216     {
217 
218         T obj = Activator.CreateInstance<T>();
219         using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
220         {
221 
222             DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
223 
224             return (T)serializer.ReadObject(ms);
225         }
226 
227     }
228 
229     //好了,我们来测试下
230     string JsonStr = "[{Name:'苹果',Price:5.5},{Name:'橘子',Price:2.5},{Name:'柿子',Price:16}]";
231     List<Product> products = new List<Product>();
232     products = JSONStringToList<Product>(JsonStr);
233     //Response.Write(products.Count());
234     foreach (var item in products)
235     {
236         Response.Write(item.Name + ":" + item.Price + "<br />");
237     }
238     public class Product
239     {
240         public string Name { get; set; }
241         public double Price { get; set; }
242     }
243     结果:
244     苹果:5.5
245     橘子:2.5
246     柿子:16
247     
248 */    
249     
250     
原文地址:https://www.cnblogs.com/hbtmwangjin/p/9143760.html