Serialization and deserialization of C#json

Source: Internet
Author: User

Original: C#json serialization and deserialization

Windows Phone learning also for some time, want to do a news client practice practiced hand, so on the internet to see if there is no interface. In the Tengu podcast found Hot Hot Word News API Open Interface, interface provides JSON format data, because before the use of JSON, so on the online various search, technical discussion group of various questions ...

No more nonsense to say, the following to share my harvest. Look like me small white less detours.

An entity class

 Public  class Newsmodel    {      publicstringgetset;}        Public string Get Set ; }        Public string Get Set ; }    }

Serialization methods

        #regionJSON serialization of entity classes/// <summary>        ///JSON serialization of entity classes/// </summary>        /// <param name= "Item" >entity class object</param>        /// <returns>JSON format string</returns>         Public Static stringTojosndata (ObjectItem) {DataContractJsonSerializer Serializer=NewDataContractJsonSerializer (item.            GetType ()); stringresult =string.            Empty; using(MemoryStream ms =NewMemoryStream ()) {Serializer.                WriteObject (MS, item); Ms. Position=0; using(StreamReader reader =NewStreamReader (MS)) {Result=Reader.                ReadToEnd (); }            }            returnresult; }         #endregion
JSON serialization of entity classes
List<newsmodel> Ltmodel =NewList<newsmodel>(); Ltmodel.add (NewNewsmodel {Title ="Title 1", Contents ="Content 1", times ="2014-12--2" }); Ltmodel.add (NewNewsmodel {Title ="Title 2", Contents ="Content 2", times ="2014-12--2" }); Ltmodel.add (NewNewsmodel {Title ="Title 3", Contents ="Content 3", times ="2014-12--2" }); Ltmodel.add (NewNewsmodel {Title ="Title 4", Contents ="Content 4", times ="2014-12--2" }); Ltmodel.add (NewNewsmodel {Title ="Title 5", Contents ="Content 5", times ="2014-12--2" }); Ltmodel.add (NewNewsmodel {Title ="Title 6", Contents ="Content 6", times ="2014-12--2" }); stringJsonstr=jsonhelper.tojosndata (Ltmodel);
The output JSON string is:

The way to sequence a Json string as a solid object is to use Newtonsoft.Json.dll, which can be downloaded on the website http://json.codeplex.com/

After introducing namespaces, it's easy.

            New List<newsmodel>();             var root = jsonconvert.deserializeobject<newsmodel[]>(jsonstr);             foreach (var in root)            {                Demodel.add (new newsmodel {Title = Item). Title, Contents = Item. Contents, times = item. Times});            

This deserializes the JSON string into a list collection.

Reference: Http://stackoverflow.com/questions/8639421/json-parsing-in-windows-phone-7.

Serialization and deserialization of C#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.