Windows Phone 六、JSON序列化
JSON序列化
1 public class Person 2 { 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 }
1 Person zhang = new Person(); 2 zhang.Id = 1; 3 zhang.Name = "letter zhang"; 4 zhang.Age = 18; 5 zhang.Gender = 1; 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 : class 37 { 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 : class 50 { 51 return Deserialization(json, typeof(TResult)) as TResult; 52 } 53 }