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