標籤:
JSON序列化
1 public class Person2 {3 public int Id { get; set; }4 public string Name { get; set; }5 public int Age { get; set; }6 public int Gender { get; set; }7 }
Person
1 Person zhang = new Person(); 2 zhang.Id = 1; 3 zhang.Name = "letter zhang"; 4 zhang.Age = 18; 5 zhang.Gender = ‘男‘; 6 //序列化工具 7 DataContractJsonSerializer serializer = new DataContractJsonSerializer(zhang.GetType()); 8 //準備一個流對象作為JSON載體 9 using (var memory = new MemoryStream())10 {11 //序列化一個對象到一個流當中12 serializer.WriteObject(memory, zhang);13 //讓流中指標的位置放到第一位14 memory.Position = 0;15 //讀取資料流中的資料16 using (var reader = new StreamReader(memory))17 {18 var result = await reader.ReadToEndAsync();19 System.Diagnostics.Debug.WriteLine(result);20 }21 }
JSON還原序列化
1 Person zhang = new Person(); 2 zhang.Id = 1; 3 zhang.Name = "letter zhang"; 4 zhang.Age = 18; 5 zhang.Gender = ‘男‘; 6 //序列化工具 7 DataContractJsonSerializer serializer = new DataContractJsonSerializer(zhang.GetType()); 8 //定義JSON結果String 9 string json;10 //準備一個流對象作為JSON載體11 using (var memory = new MemoryStream())12 {13 //序列化一個對象到一個流當中14 serializer.WriteObject(memory, zhang);15 //讓流中指標的位置放到第一位16 memory.Position = 0;17 //讀取資料流中的資料18 using (var reader = new StreamReader(memory))19 {20 json = await reader.ReadToEndAsync();21 }22 }23 //還原序列化工具24 DataContractJsonSerializer serializer2 = new DataContractJsonSerializer(typeof(Person));25 var buffer = Encoding.UTF8.GetBytes(json);26 //根據位元組數組建立一個流27 using (var stream = new MemoryStream(buffer))28 {29 //讀取流對象中的Person對象30 var p2 = serializer2.ReadObject(stream) as Person;31 if (p2 != null)32 {33 System.Diagnostics.Debug.WriteLine(p2.Name);34 }35 }
JsonHelper封裝
1 public static class JsonHelper 2 { 3 //序列化方法 4 public async static Task<string> Serialization(object obj) 5 { 6 DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType()); 7 //準備一個流對象作為JSON載體 8 using (var memory = new MemoryStream()) 9 {10 //序列化一個對象到一個流中11 serializer.WriteObject(memory, obj);12 //讓流中指標的位置放到第一位13 memory.Position = 0;14 //讀取資料流中的資料15 using (var reader = new StreamReader(memory))16 {17 string json = await reader.ReadToEndAsync();18 return json;19 }20 }21 }22 //還原序列化方法23 public static object Deserialization(string json, Type resultType)24 {25 DataContractJsonSerializer serializer2 = new DataContractJsonSerializer(resultType);26 var buffer = Encoding.UTF8.GetBytes(json);27 //根據位元組數組建立一個流28 using (var stream = new MemoryStream(buffer))29 {30 //讀取流對象中的Person對象31 var p2 = serializer2.ReadObject(stream);32 return p2;33 }34 }35 //泛型還原序列化36 public static TResult Deserialization<TResult>(string json) where TResult : class37 {38 DataContractJsonSerializer serializer2 = new DataContractJsonSerializer(typeof(TResult));39 var buffer = Encoding.UTF8.GetBytes(json);40 //根據位元組數組建立一個流41 using (var stream = new MemoryStream(buffer))42 {43 //讀取流對象中的Person對象44 var p2 = serializer2.ReadObject(stream) as TResult;45 return p2;46 }47 }48 //重載泛型方法調用非泛型49 public static TResult Deserialization<TResult>(string json) where TResult : class50 {51 return Deserialization(json, typeof(TResult)) as TResult;52 }53 }
JsonHelper
Windows Phone 六、JSON