. Net json serialization component Json. NET

Source: Internet
Author: User

Json. NET (Newtonsoft. json) Yes. net Framework is a popular efficient json serialization open-source component.. Net Framework 2.0 to 4.5 +, and can be used.. Net environments, such as Asp.net, Silverlight, Windows Phone, and Windows 8. more features step-by-step open-source performance

Compare the performance test results of Json. NET, DataContractJsonSerializer, and JavascriptSeriallizer.

 

Reference
Method 1. Download and decompress the reference Newtonsoft. Json. dll

Method 2: Nuget Installation

PM> Install-Package Newtonsoft. Json

 

Serialization and deserialization

1. Basic usage: first reference the Newtonsoft. Json namespace and define the class with the same structure as json for conversion.


Software software = new Software {SoftID = 1,
SoftName = "free time ",
DownloadUrl = "http://itunes.apple.com/cn/app/id427577372? Mt = 8 ",
ReleaseTime = DateTime. Now
};

// Serialization
String jsonStr = JsonConvert. SerializeObject (software );

// Deserialization
Software objSoftware = JsonConvert. DeserializeObject <Software> (jsonStr );
Console. WriteLine (jsonStr); serialized output

 

 

2. Time Format processing. By default, the serialization of the DateTime type is serialized as above. This format is hard to read from other clients, or you want to format it by yourself.

Newtonsoft. Json. Converters. IsoDateTimeConverter timeConverter = new Newtonsoft. Json. Converters. IsoDateTimeConverter ();
TimeConverter. DateTimeFormat = "yyyy-MM-dd HH: mm: ss ";
Console. WriteLine (JsonConvert. SerializeObject (software, timeConverter); output result:

 

 

3. Anonymous serialization. This method can be deserialized without defining classes with the same json structure in advance.


// Json string
String jsonStr = @ "{result:-1, desc: 'parameter error. Check the format '}";

// Deserialization
Var obj = JsonConvert. DeserializeAnonymousType (jsonStr, new {result = 0, desc = string. Empty });
Console. WriteLine (string. Format ("result: {0} desc: {1}", obj. result, obj. desc ));

4. Quickly locate the node for fast processing or strings with complicated json structures, and do not want to define the corresponding transfer class, as shown in


{"Weatherinfo": {"city": "fuzhou", "city_en": "fuzhou", "date_y": "August May 4, 2013", "date ":"", "week": "Saturday", "fchh": "18", "cityid": "101230101", "temp1": "16 ℃ ~ 21 ℃ "," temp2 ":" 16 ℃ ~ 23 ℃ "," temp3 ":" 17 ℃ ~ 24 ℃ "," temp4 ":" 16 ℃ ~ 26 ℃ "," temp5 ":" 17 ℃ ~ 29 ℃ "," temp6 ":" 18 ℃ ~ 28 ℃ "," tempF1 ":" 60.8 bytes ~ 69.8 bytes "," tempF2 ":" 60.8 bytes ~ 73.4 bytes "," tempF3 ":" 62.6 bytes ~ 75.2 bytes "," tempF4 ":" 60.8 bytes ~ 78.8 bytes "," tempF5 ":" 62.6 bytes ~ 84.2 bytes "," tempF6 ":" 64.4 bytes ~ 82.4 Tib "," weather1 ":" Shower "," Weather ":" Shower Overcast "," weather3 ":" Overcast to thundershower "," weather4 ": "Shower to thundershower", "weather5": "shower to cloudy", "weather6": "Cloudy to moderate rain", "img1": "3", "img2 ": "99", "img3": "3", "img4": "2", "img5": "2", "img6": "4", "img7 ": "3", "img8": "4", "img9": "3", "img10": "1", "img11": "1", "img12 ": "8", "img_single": "3", "img_title1": "Shower", "img_title2": "Shower", "img_title3": "Shower", "img_title4 ": "Overcast", "img_title5": "Overcast", "img_title6": "thundershower", "img_title7": "Shower", "img _ Title8 ":" thundershower "," img_title9 ":" Shower "," img_title10 ":" multi cloud "," img_title11 ":" multi cloud "," img_title12 ":" moderate rain ", "img_title_single": "Shower", "wind1": "Breeze", "wind2": "Breeze", "wind3": "Breeze", "wind4": "Breeze ", "wind5": "Breeze", "wind6": "Breeze", "fx1": "Breeze", "fx2": "Breeze", "fl1 ": "less than 3", "fl2": "less than 3", "fl3": "less than 3", "fl4": "less than 3", "fl5 ": "less than 3", "fl6": "less than 3", "index": "comfortable", "index_d ": "it is recommended that you have a slim suit or denim shirt and other transitional clothes in spring and autumn. Suits and jackets are suitable for the elderly and the weak. "," Index48 ":" comfortable "," index48_d ":" It is recommended that you have a thin suit or denim shirt and other spring and autumn transitional clothes. Suits and jackets are suitable for the elderly and the weak. "," Index_uv ":" weak "," index48_uv ":" weakest "," index_xc ":" Not suitable "," index_tr ":" suitable "," index_co ": "comfortable", "st1": "19", "st2": "14", "st3": "25", "st4": "14", "st5 ": "23", "st6": "16", "index_cl": "Not suitable", "index_ls": "Not suitable", "index_ag ": "Easy to use"} Read weather1 under weatherinfo

Var obj = JObject. Parse (html );
String weather1 = (string) obj ["weatherinfo"] ["weather1"];

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.