Serialization and deserialization of JSON in ASP (i)

Source: Internet
Author: User

JSON is a data format designed specifically for JavaScript code that runs on Web pages in a browser. There are more and more scenarios for using JSON in Web site applications, this article describes the serialization and deserialization of JSON in ASP, mainly a brief introduction to JSON, how to process the serialization and deserialization of ASP, and the processing of datetime, set, and dictionary in serialization and deserialization.

I. Introduction of JSON

JSON (JavaScript object Notation,javascript) is a lightweight data interchange format.

JSON is a collection of "name-value pairs". The structure consists of braces ' {} ', brackets ' [] ', comma ', ', ' colon ': ', ' double quotation marks ' "", consisting of a data type of object,number,boolean,string,array, NULL, etc.

JSON has the following form:

Object is an unordered set of "name-value pairs," and an object ends with "{" Starting with "}". After each "name" followed by a ":", multiple "name value pairs" are separated by commas. Such as:

var user={"name": "Zhang San", "gender": "Male", "Birthday": "1980-8-8"}

An array is an ordered set of values, an array that begins with "[", Ends with "]", and separates values by ",". Such as:

var userlist=[{"user": {"name": "Zhang San", "gender": "Male", "Birthday": "1980-8-8"}},{"user": {"name": "John Doe", "gender": "Male", " Birthday ":" 1985-5-8 "}}];

A string is a collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash.

Ii. serialization and deserialization of JSON data

You can use the DataContractJsonSerializer class to serialize a type instance to a JSON string and deserialize the JSON string into a type instance. DataContractJsonSerializer under the System.Runtime.Serialization.Json namespace, the. NET Framework 3.5 Included in System.ServiceModel.Web.dll, you need to add a reference to it;. NET Framework 4 in System.Runtime.Serialization.

Using DataContractJsonSerializer to serialize and deserialize code:

   1:using System;
   2:using System.Collections.Generic;
   3:using System.Linq;
   4:using system.web;
   5:using System.Runtime.Serialization.Json;
   6:using System.IO;
   7:using System.Text;
   
   9://<summary>
  ///JSON serialization and deserialization helper classes
  One://</summary>
  12:public class Jsonhelper
  13: {
  ///     <summary>
  ://     JSON serialization
  ://     </summary>
  +: Public     static string jsonserializer<t> (T t)
  :     {
  :         datacontractjsonserializer ser = new DataContractJsonSerializer (typeof (T));
  :         MemoryStream ms = new MemoryStream ();
  :         ser. WriteObject (MS, T);
  A:         string jsonstring = Encoding.UTF8.GetString (ms. ToArray ());
  At:         Ms. Close ();
  :         return jsonstring;
  :     }
  
  +//     <summary>
  ///     JSON deserialization
  ://     </summary>
  : Public     static T jsondeserialize<t> (String jsonstring)
  :     {
  :         datacontractjsonserializer ser = new DataContractJsonSerializer (typeof (T));
  :         MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring));
  :         t obj = (t) ser. ReadObject (MS);
  :         return obj;
  :     }
  37:}

Serialization Demo:

Simple Object Person:

   1:public class Person
   2: {
   3: Public     string Name {get; set;}
   4: Public     int Age {get; set;}
   5:}

Serialized as a JSON string:

   1:protected void Page_Load (object sender, EventArgs e)
   2: {
   3: Person     p = new person ();
   4:     p.name = "Zhang San";
   5:     p.age = 28;
    
    7:     String jsonstring = Jsonhelper.jsonserializer<person> (p);
    8:     Response.Write (jsonstring);
   9:}

Output Result:

Deserialization Demo:

   1:protected void Page_Load (object sender, EventArgs e)
   2: {
   3:     String jsonstring = "{\" age\ ": 28,\" name\ ": \" Zhang San \ "}";
   4: Person     p = jsonhelper.jsondeserialize<person> (jsonstring);
   5:}

Serialization and deserialization of JSON in ASP (i)

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.