Skip to content

Format list of objects as JSON #357

Answered by axunonb
vhafdal asked this question in Q&A
Discussion options

You must be logged in to vote

Here's the the proof-of-concept to hammer a nail into the wall with pliers 😉, i.e. to use SmartFormat to serialize to JSON. It's possible, but pretty tedious:

public class Product
{
    public string ItemCode { get; set; }
    public string Description { get; set; }
    public decimal Price1 { get; set; }
}
public void JsonOutput()
{
    var p1 = new Product { ItemCode = "0001", Description = "Item 1", Price1 = 100 };
    var p2 = new Product { ItemCode = "0002", Description = "Item 2", Price1 = 110 };
    var arrayOfProducts = new List<Product> { p1, p2 };

    var result = Smart.Format("[{:list:\n\\{\n\"Id:\"\\{\"SKU\":\"{:{ItemCode}\"\\}}|\n\\},}\n\\}\n]", arrayOfProducts);
    /*

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by axunonb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants