JSON operations in ASP.

Source: Internet
Author: User

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.

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.