Click or drag to resize
Json.NET

Create JSON from an Anonymous Type

 

This sample creates a JObject from an anonymous type.

Sample
Types
public class Post
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Link { get; set; }
    public IList<string> Categories { get; set; }
}
Usage
List<Post> posts = new List<Post>
{
    new Post
    {
        Title = "Episode VII",
        Description = "Episode VII production",
        Categories = new List<string>
        {
            "episode-vii",
            "movie"
        },
        Link = "episode-vii-production.aspx"
    }
};

JObject o = JObject.FromObject(new
{
    channel = new
    {
        title = "Star Wars",
        link = "http://www.starwars.com",
        description = "Star Wars blog.",
        item =
            from p in posts
            orderby p.Title
            select new
            {
                title = p.Title,
                description = p.Description,
                link = p.Link,
                category = p.Categories
            }
    }
});

Console.WriteLine(o.ToString());
// {
//   "channel": {
//     "title": "Star Wars",
//     "link": "http://www.starwars.com",
//     "description": "Star Wars blog.",
//     "item": [
//       {
//         "title": "Episode VII",
//         "description": "Episode VII production",
//         "link": "episode-vii-production.aspx",
//         "category": [
//           "episode-vii",
//           "movie"
//         ]
//       }
//     ]
//   }
// }