Use of Newtonsoft.json

Source: Internet
Author: User

JSON, or JavaScript Object natation, is a lightweight data interchange format that is ideal for server-to-javascript interaction. As with XML, JSON is also a data format based on plain text. For more information, please click here: https://www.ibm.com/developerworks/cn/web/wa-lo-json/since JSON is inherently prepared for JavaScript, the JSON data format is very simple and you can use the JSON Transfer a simple string,number,boolean, or you can transfer an array, or a complex object.Under the. NET environment, we use json.net to serialize and deserialize JSON data. First click Connect HTTP://SOURCEFORGE.NET/PROJECTS/CSJSON/?SOURCE=DLP to download the JSON. NET plug-ins and code. then make a reference in the project Newtonsoft.Json.dllAdd a namespace: using Newtonsoft.json;Here are some important methods and examples of JSON serialization and deserialization:
// Serialization of Jsonconvert.serializeobject (object  value)// overloaded method Jsonconvert.serializeobject ( Object params jsonconverter[] Converters) // deserialization Jsonconvert.deserializeobject (string  value, type type)//  Overloaded method Jsonconvert.deserializeobject (string value, type type, params jsonconverter[] converters)
These two methods can implement basic serialization and deserialization requirements, see the following example: First, we first build a person class code as follows:
  Public classPerson {Private stringname;  Public stringName {Get{returnname;} Set{name =value;} }        Private intAge ;  Public intAge {Get{returnAge ;} Set{age =value;} }    }

Serialization of

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingNewtonsoft.json;namespacejsonnet{ Public Partial classTest:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { person person=NewPerson (); Person. Name="Goldeneasy"; Person. Age= -; stringStrserializejson =Jsonconvert.serializeobject (person);                             Response.Write (Strserializejson); }    }}
Output Result:{"Name": "Goldeneasy", "Age": 25} Deserialization:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingNewtonsoft.json;namespacejsonnet{ Public Partial classTest:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { person person=NewPerson (); Person. Name="Goldeneasy"; Person. Age= -; stringStrserializejson =Jsonconvert.serializeobject (person); Person User= (person) jsonconvert.deserializeobject (Strserializejson,typeof(person)); Response.Write (user.                 Name); }    }}

The output is: Goldeneasy

Use of Newtonsoft.json

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.