views:

85

answers:

1

I have a list of person objects which I want to send in response to a jquery' ajax request. I want to send the list into JSON format. The list is as follows -

List<Person> PersonList = new List<Person>();

Person Atiq = new Person("Atiq Hasan Mollah", 23, "Whassap Homie");
Person Sajib = new Person("Sajib Mamud", 24, "Chol kheye ashi");

PersonList.Add(Atiq);
PersonList.Add(Sajib);

How can I convert the "PersonList" list into JSON ?

+2  A: 

JSON.NET has worked really well for me. http://www.codeplex.com/Json

And if you are doing Web Forms and JQuery then this link might help you out:

http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

Daniel Lee
I tried using JSON.NET's "JsonConvert.SerializeObject(PersonList)" method. But when I send ajax request using jquery's "$.getJSON(...)" method, it causes an "Invalid JSON" error, which I checked using Firebug.
Night Shade
Using Web Forms with JQuery's ajax functionality is a bit tricky that's why I included the encosia link. getJSON is a shorthand function for the ajax method but getJSON does not work with Web Forms you must use the $.ajax() method. Also you must set the encoding correctly and the type must be Post and watch out for the .d security measure that ASP.NET uses to protect against an XSS attack. See this article: http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/
Daniel Lee