C# 結構體和List類型數據轉Json數據保存和讀取

C#  結構體和List<T>類型數據轉Json數據保存和讀取javascript

  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     
相關文章
相關標籤/搜索