C# code to serialize plain-old-CLR-objects to JSON

Jonathan picture Jonathan · Sep 29, 2009 · Viewed 7.9k times · Source

Within an ASP.NET application, I'd like to serialize a collection of plain-old-CLR-objects (POCO) to a JSON string, which will then be sent down to the client as part of a web response.

Is there a light-weight C# library that does this?

Answer

Jon Skeet picture Jon Skeet · Sep 29, 2009

Yes, I've had a lot of success with JSON.NET.

As an example from the web page:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };

string json = JsonConvert.SerializeObject(product);