JSON serialization
1 Public class Person2 {3 Public intId {Get;Set; }4 Public stringName {Get;Set; }5 Public intAge {Get;Set; }6 Public intGender {Get;Set; }7}
Person
1Person Zhang =NewPerson ();2Zhang. Id =1;3Zhang. Name ="Letter Zhang";4Zhang. Age = -;5Zhang. Gender ='male';6 //Serialization Tools7DataContractJsonSerializer serializer =NewDataContractJsonSerializer (Zhang. GetType ());8 //prepare a Stream object as a JSON vector9 using(varMemory =NewMemoryStream ())Ten { One //serializes an object into a stream A Serializer. WriteObject (memory, Zhang); - //put the position of the pointer in the stream to the first bit -Memory. Position =0; the //reading data from a data stream - using(varReader =NewStreamReader (memory)) - { - varresult =awaitReader. Readtoendasync (); + System.Diagnostics.Debug.WriteLine (result); - } +}
JSON deserialization
1Person Zhang =NewPerson ();2Zhang. Id =1;3Zhang. Name ="Letter Zhang";4Zhang. Age = -;5Zhang. Gender ='male';6 //Serialization Tools7DataContractJsonSerializer serializer =NewDataContractJsonSerializer (Zhang. GetType ());8 //To define a JSON result string9 stringJSON;Ten //prepare a Stream object as a JSON vector One using(varMemory =NewMemoryStream ()) A { - //serializes an object into a stream - Serializer. WriteObject (memory, Zhang); the //put the position of the pointer in the stream to the first bit -Memory. Position =0; - //reading data from a data stream - using(varReader =NewStreamReader (memory)) + { -JSON =awaitReader. Readtoendasync (); + } A } at //Deserialization Tool -DataContractJsonSerializer Serializer2 =NewDataContractJsonSerializer (typeof(person)); - varBuffer =Encoding.UTF8.GetBytes (JSON); - //Create a stream based on a byte array - using(varstream =NewMemoryStream (buffer)) - { in //reading a person object in a Stream object - varP2 = serializer2. ReadObject (Stream) asPerson ; to if(P2! =NULL) + { - System.Diagnostics.Debug.WriteLine (p2. Name); the } *}
Jsonhelper Package
1 Public Static classJsonhelper2 {3 //Serialization Methods4 Public Async Statictask<string> Serialization (Objectobj)5 {6DataContractJsonSerializer serializer =NewDataContractJsonSerializer (obj. GetType ());7 //prepare a Stream object as a JSON vector8 using(varMemory =NewMemoryStream ())9 {Ten //serializes an object into a stream One Serializer. WriteObject (memory, obj); A //put the position of the pointer in the stream to the first bit -Memory. Position =0; - //reading data from a data stream the using(varReader =NewStreamReader (memory)) - { - stringJSON =awaitReader. Readtoendasync (); - returnJSON; + } - } + } A //Deserialization Method at Public Static ObjectDeserialization (stringjson, Type Resulttype) - { -DataContractJsonSerializer Serializer2 =NewDataContractJsonSerializer (resulttype); - varBuffer =Encoding.UTF8.GetBytes (JSON); - //Create a stream based on a byte array - using(varstream =NewMemoryStream (buffer)) in { - //reading a person object in a Stream object to varP2 =Serializer2. ReadObject (stream); + returnP2; - } the } * //Generic deserialization $ Public StaticTResult deserialization<tresult> (stringJsonwhereTResult:classPanax Notoginseng { -DataContractJsonSerializer Serializer2 =NewDataContractJsonSerializer (typeof(TResult)); the varBuffer =Encoding.UTF8.GetBytes (JSON); + //Create a stream based on a byte array A using(varstream =NewMemoryStream (buffer)) the { + //reading a person object in a Stream object - varP2 = serializer2. ReadObject (Stream) asTResult; $ returnP2; $ } - } - //overloading generic method calls non-generics the Public StaticTResult deserialization<tresult> (stringJsonwhereTResult:class - {Wuyi returnDeserialization (JSON,typeof(TResult)) asTResult; the } -}
Jsonhelper
Windows Phone VI, JSON