Windows Phone VI, JSON

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.