First of all, we have to understand what is JSON, to learn more about the JSON aspects of the information you can click Https://www.ibm.com/developerworks/cn/web/wa-lo-json/, I am here to briefly introduce the next Json:json that JavaScript Object Natation, a lightweight data interchange format, is ideal for server-to-JavaScript interaction. As with XML, JSON is also a data format based on plain text. Since JSON is inherently prepared for JavaScript, the JSON data format is very simple, you can transfer a simple string,number,boolean with JSON, 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.dll add namespace: Using Newtonsoft.json; Here are some important methods and examples of JSON serialization and deserialization:Jsonconvert.serializeobject (object value)serialization, it has an overloaded methodJsonconvert.serializeobject (object value, params jsonconverter[] converters).
Jsonconvert.deserializeobject (String value, type type),Deserialization, it has an overloaded methodJsonconvert.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 class person
{
private string name;
public string Name
{
get {return name;}
set {name = value;}
} private int age;
public int Age
{
get {return age;}
set {age = value;}
}
}1) SerializationUsing System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Newtonsoft.json; namespace jsonnet
{
public partial class test: System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
person person = new person ();
person. Name = "Goldeneasy";
person. Age = 25;
string StrSerializeJSON = Jsonconvert.serializeobject (person);
response.write (StrSerializeJSON); }
}
}Output Result:{"Name": "Goldeneasy", "Age": 25}2) DeserializationUsing System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using Newtonsoft.json; Namespace Jsonnet
{
public partial class Test:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
person person = new person ();
Person. Name = "Goldeneasy";
Person. Age = 25;
String Strserializejson = Jsonconvert.serializeobject (person);
Person user = (person) jsonconvert.deserializeobject (Strserializejson, typeof (person));
Response.Write (user. Name);
}
}
The output result is: goldeneasyhttp://blog.sina.com.cn/s/blog_70686f3a0101kemg.html
Use Newtonsoft.json to convert, read, write JSON in. Net