JSON Serialization and Deserialization in ASP.Net

本文介绍如何使用ASP.NET进行JSON序列化和反序列化操作,包括将对象转换为JSON字符串及从JSON字符串还原对象的过程。通过一个Person类实例演示了序列化与反序列化的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    

JSON Serialization and Deserialization in ASP.Net
 

I was looking around for a simple example which would just do an object serialization to a JSON format, and then deserializing back to the original object. I found few examples on MSDN, but did seem to be too long to try. Here I've given a simple code which would make your understanding easy, and simpler.

 

 

What is JSON?

 

JSON is another format of expressing data, just like XML. But, JSON is very simpler than XML, and tiny than XML. So, it is becoming popular in the web world to choose the JSON notation over XML since JSON notation are usually shorter, and less data to be transmitted if at all they were to be.

 

Okay, I understood a little about JSON. Give me an example!

 

I know an example will get your understanding much better. Below is a simple example of how an object can be expressed in JSON notation. Let's take a classical example of a Person object, and expressing myself as an object.

{

   "firstName": "Rakki",

   "lastName":"Muthukumar",

   "department":"Microsoft PSS",

   "address": {

      "addressline1": "Microsoft India GTSC",

      "addressline2": "PSS - DSI",

      "city": "Bangalore",

      "state": "Karnataka",

      "country": "India",

      "pin": 560028

   }

   "technologies": ["IIS", "ASP.NET","JavaScript","AJAX"]

}

In the above example, address is another object inside my Person, and technologies is an array or strings.

 

Express this as a simple .NET class, please!

 

Here is a simple example.

public class Address

{

    public string addressline1, addressline2, city, state, country;

    public int pin;

}

 

public class Person

{

    public string firstName, lastName, department;

    public Address address = new Address();

    public string[] technologies;

}

 

I get it. Now what? JSON Serialization / Deserialization?

 

I'm sure you do not want me to write what is serialization, and deserialization here. I'll touch upon how to use System.Web.Script.Serialization.JavaScriptSerializer to convert an existing object into a JSON string.

JavaScriptSerializer js = new JavaScriptSerializer();

Person p1 = new Person();

p1.firstName = "Rakki";

p1.lastName = "Muthukumar";

p1.department = "Microsoft PSS";

p1.address.addressline1 = "Microsoft India GTSC";

p1.address.addressline2 = "PSS - DSI";

p1.address.city = "Bangalore";

p1.address.state = "Karnataka";

p1.address.country = "India";

p1.address.pin = 560028;

p1.technologies = new string[] { "IIS", "ASP.NET", "JavaScript", "AJAX" };

 

string str = js.Serialize(p1);

Above code just creates a Person object, and assign some values. Look at the last line where we are actually doing a serialization - means dumping the contents of the object to a JSON notation. Below is the string produced by the above code:

 

{"firstName":"Rakki","lastName":"Muthukumar","department":"Microsoft PSS","address":{"addressline1":"Microsoft India GTSC","addressline2":"PSS - DSI","city":"Bangalore","state":"Karnataka","country":"India","pin":560028},"technologies":["IIS","ASP.NET","JavaScript","AJAX"]}

 

It is the same as how we defined the object before - but in a single line. Now, you have successfully converted your object into a JSON string.

 

Now how do I deserialize it?

 

Before going into how to deserialize it, let's just understand when you might want to deserialize the object. Classical example is a JSON object returned from a web service, or from your client side javascript code, and you will be able to deserialize it in the server side to use the received object. 

 

Person p2 = js.Deserialize<Person>(str);

Response.Write(p2.lastName);

 

p2.lastName would contain "Muthukumar" if your deserialization works fine. I suggest you to use the Deserialize<T> method since it will reduce the type casting you might be doing if you use DeserializeObject() method which returns a System.Object.

Hope this helps!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值