The recent project needs to use the JSON operation, Google a bit to find a few better ways to do ....
I. using the. NET Framework's own JSON operation method provided by Mircosoft
1. Using JavaScriptSerializer, located in the namespace System.Web.Script.Serialization, use the following:
Serialized as a JSON string:
C # code
User User = new User {Name = "jquery", age = 20};
JavaScriptSerializer serializer = new JavaScriptSerializer ();
string result = Serializer. Serialize (user);
Deserializing JSON
C # code
String input = "";
JavaScriptSerializer serializer = new JavaScriptSerializer ();
Serializer. Deserialize (input);
2. Using the DataContractJsonSerializer class,
It is recommended to use this, which is up-to-date, located under the namespace System.Runtime.Serialization.Json, where serialization and deserialization call its writeobject () and ReadObject () methods.
II: Use of third-party json.net (http://json.codeplex.com/)
Version:Json.NET 3.5 Release 7
Date:fri APR at 5:00pm
Add a Newtonsoft.Json.dll reference to the project:
Using Newtonsoft.json;
Using Newtonsoft.Json.Converters;
Deserializes a JSON string into an object
Target object = Jsonconvert.deserializeobject (JSON string, typeof (target object));
Serializes the target object into a JSON string
String JSON string = Jsonconvert.serializeobject (target object);
String jsontext = "";
JSON read
Jsontext = "[' json! ', 1,true,{property: ' Value '}]";
Jsonreader reader = new JsonTextReader (new StringReader (Jsontext));
Console.WriteLine ("Tokentype\t\tvaluetype\t\tvalue");
while (reader. Read ())
{
Console.WriteLine (reader. Tokentype + "\t\t" + reader. ValueType + "\t\t" + reader. Value);
Console.WriteLine ("\n\r");
}
JSON write
StringWriter SW = new StringWriter ();
Jsonwriter writer = new JsonTextWriter (SW);
Writer. Writestartarray ();
Writer. WriteValue ("json!");
Writer. WriteValue (1);
Writer. WriteValue (TRUE);
Writer. Writestartobject ();
Writer. Writepropertyname ("property");
Writer. WriteValue ("value");
Writer. Writeendobject ();
Writer. Writeendarray ();
Writer. Flush ();
Jsontext = SW. Getstringbuilder (). ToString ();
Console.WriteLine (Jsontext);
Links and information about some of the relevant JSON operations
JSON official: http://www.json.org/json-zh.html
Another open source Json class Library: Jayrock.json (http://www.cnblogs.com/chjw8016/archive/2010/04/20/1716198.html?login=1#commentform )
JSON operations in ASP.