Windows Phone 六、JSON

來源:互聯網
上載者:User

標籤:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.