{ “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”] }
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;}
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;
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);
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.
Person p2 = js.Deserialize<Person>(str);Response.Write(p2.lastName);
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!
thanks for giving this article, it helps me a lot.
Wonderful and very simple explanation for people who don't know what JSON is. I am one among them......If there is rating then my 5 goes to it...
Thanks Rakki for taking the time to post this straight-forward and simple to understand article; I like your style of writing. Keep up the good work!
Very neat, very simple, very easy to understand and very easy to use. Thank you.
Very nice start to JSON for me! thanks
this made JSON very simple to understand
That is great. Lowered my blood pressure a little bit.
i have been looking for this stuff from nearly 1 week,it cleared me so many things,keep up the good work n plz upload few more stuff on Ajax....
I have been looking for the most basic explanation of JSON and Serialization / Deserialization concept in .NET for hours. Thanks for posting this great introductory article. I am sure it will help many developers to get started on JSON!
Good introductory article. I really enjoy reading this blog post.