2

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 X = new Person("X", 23, "This is X");
Person Y = new Person("Y", 24, "This is Y");

PersonList.Add(X);
PersonList.Add(Y);

How can I convert this PersonList list into JSON ?

1 Answer 1

3

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/

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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: encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again
Just downloaded Json.NET with nuget. Had it working in minutes. It's awesome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.