This sample uses LINQ to JSON to manually convert a .NET type to JSON.
public class BlogPost
{
public string Title { get; set; }
public string AuthorName { get; set; }
public string AuthorTwitter { get; set; }
public string Body { get; set; }
public DateTime PostedDate { get; set; }
}
var blogPosts = new List<BlogPost>
{
new()
{
Title = "Json.NET is awesome!",
AuthorName = "James Newton-King",
AuthorTwitter = "JamesNK",
PostedDate = new(2013, 1, 23, 19, 30, 0),
Body = "<h3>Title!</h3><p>Content!</p>"
}
};
var blogPostsArray = new JArray(
blogPosts.Select(p => new JObject
{
{"Title", p.Title},
{
"Author", new JObject
{
{"Name", p.AuthorName},
{"Twitter", p.AuthorTwitter}
}
},
{"Date", p.PostedDate},
{"BodyHtml", HttpUtility.HtmlEncode(p.Body)}
})
);
Console.WriteLine(blogPostsArray.ToString());
// [
// {
// "Title": "Json.NET is awesome!",
// "Author": {
// "Name": "James Newton-King",
// "Twitter": "JamesNK"
// },
// "Date": "2013-01-23T19:30:00",
// "BodyHtml": "<h3>Title!</h3><p>Content!</p>"
// }
// ]