JSON Serialization and Deserialization in ASP.NET

This article focuses on JSON Serialization and Deserialization in ASP.NET.

JSON is one kind of data format which is designer for running JavaScript on websites. At present, JSON is widely used in web. This article focuses on JSON Serialization and Deserialization in ASP.NET, including the brief introduction of JSON, how to serialize and deserialize with ASP.NET and operation on date, assembly, dictionary.

String: Any quantity unicode character assembly which is enclosed with quotation marks. It uses backslash to escape.

2. Serialize and Deserialize JSON Data

We can use DataContractJsonSerializer to serialize type instance to JSON string and deserialize JSON string to type instance. DataContractJsonSerializer is under System.Runtime.Serialization.Json namespace. It is included in System.ServiceModel.Web.dll in .NET Framework 3.5 and System.Runtime.Serialization in .NET Framework 4.0. We need to add it as reference.

Reserialization Demo

Result

In ASP.NET, JSON serializaation and deserialization can use JavaScriptSerializer which is under System.Web.Script.Serialization namespace. We need to add System.Web.Extensions.dll as reference or use JSON.NET.

3. JSON Serialization and Deserialization on DateTime

JSON cannot support date and time directly. The value of DateTime is shown as "/Date(700000+0500)/". The first number (700000) stands for milliseconds from Jan. 1, 1970 according to base time (not saylight saving time) in GMT. The number can be negative to present time before Jan. 1, 1970. The part "+0500" is optional. It present the time is Local, in other words, it can be converted to local time zone when deserializing. If this part is not there, the time will be deserialized as UTC.