Introduction: JSON is a data format designed specifically for JavaScript code running on web pages in a browser. More and more scenarios are using JSON in website applications. This article introduces ASP. net json serialization and deserialization, mainly a brief introduction to JSON, ASP.. NET how to serialize and deserialize the processing of date, set, and dictionary in serialization and deserialization.
1. Introduction to JSON
JSON (JavaScript Object Notation, JavaScript Object Notation) is a lightweight data exchange format.
JSON is a set of "name-value pairs. The structure consists of braces {}, braces [], commas, colons:, and double quotation marks ("). The data types include Object, Number, Boolean, String, Array, and NULL.
JSON has the following forms:
An Object is an unordered set of "name-value pairs". An Object starts with "{" and ends. Each "name" is followed by one ":". Multiple "name-value pairs" are separated by commas. For example:
Var user = {"name": "Zhang San", "gender": "male", "birthday": "1980-8-8 "}
An Array is an ordered set of values. An Array starts with "[" and ends with "]" and is separated. For example:
Var userlist = [{"user": {"name": "Zhang San", "gender": "male", "birthday": "1980-8-8 "}}, {"user": {"name": "", "gender": "male", "birthday": "1985-5-8"}];
A String is a collection of any number of Unicode characters enclosed by double quotes. It is escaped using a backslash.
Ii. serialize and deserialize JSON data
You can use the DataContractJsonSerializer class to serialize a type instance to a JSON string and deserialize the JSON string to a type instance. DataContractJsonSerializer in System. runtime. serialization. in the Json namespace ,. NET Framework 3.5 is included in System. serviceModel. web. dll, you need to add a reference to it ;. NET Framework 4 in the System. runtime. serialization.
Code for serialization and deserialization using DataContractJsonSerializer:
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;
8:
9: // <summary>
10: // JSON serialization and deserialization helper class
11: // </summary>
12: public class JsonHelper
13 :{
14: // <summary>
15: // JSON serialization
16: // </summary>
17: public static string JsonSerializer <T> (T t)
18 :{
19: DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T ));
20: MemoryStream MS = new MemoryStream ();
21: ser. WriteObject (MS, t );
22: string jsonString = Encoding. UTF8.GetString (ms. ToArray ());
23: ms. Close ();
24: return jsonString;
25 :}
26:
27: // <summary>
28: // JSON deserialization
29: // </summary>
30: public static T JsonDeserialize <T> (string jsonString)
31 :{
32: DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T ));
33: MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (jsonString ));
34: T obj = (T) ser. ReadObject (MS );
35: return obj;
36 :}
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 = "James ";
5: p. Age = 28;
6:
7: string jsonString = JsonHelper. JsonSerializer <Person> (p );
8: Response. Write (jsonString );
9 :}
Output result:
{"Age": 28, "Name": "Zhang San "}
Deserialization Demo:
1: protected void Page_Load (object sender, EventArgs e)
2 :{
3: string jsonString = "{" Age ": 28," Name ":" James "}";
4: Person p = JsonHelper. JsonDeserialize <Person> (jsonString );
5 :}
Running result:
ASP. you can also use JavaScriptSerializer for JSON serialization and deserialization in. NET. web. script. in the Serializatioin namespace, System must be referenced. web. extensions. dll. you can also use JSON. NET.
Iii. Processing of JSON serialization and deserialization Date and Time
JSON format does not support date and time. The DateTime value is displayed as a JSON string in the format of "/Date (700000 + 0500)/", where the first number (700000 In the example provided) it is the number of milliseconds that have elapsed since midnight, January 1, January 1, 1970 by normal time (non-timeout. This number can be a negative number to indicate the previous time. The example contains "+ 0500", which indicates that the time belongs to the Local type, that is, it should be converted to the Local time zone during deserialization. If this part does not exist, the time is deserialized to Utc.
Modify the Person class and add LastLoginTime:
1: public class Person
2 :{
3: public string Name {get; set ;}
4: public int Age {get; set ;}
5: public DateTime LastLoginTime {get; set ;}
6 :}
1: Person p = new Person ();
2: p. Name = "James ";
3: p. Age = 28;
4: p. LastLoginTime = DateTime. Now;
5:
6: string jsonString = JsonHelper. JsonSerializer <Person> (p );
Serialization result:
{"Age": 28, "LastLoginTime": "/Date (1294499956278 + 0800)/", "Name": "James "}
1. Use a regular expression to replace the regular expression in the background. Modify JsonHelper:
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;
8: using System. Text. RegularExpressions;
9:
10: // <summary>
11: // JSON serialization and deserialization helper class
12: // </summary>
13: public class JsonHelper
14 :{
15: // <summary>
16: // JSON serialization
17: // </summary>
18: public static string JsonSerializer <T> (T t)
19 :{
20: DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T ));
21: MemoryStream MS = new MemoryStream ();
22: ser. WriteO