-
Hi, var ArrayOfProducts = new List(){ Now I would like this to be output a Json string that I can customize var res = SmartFormat.Smart.Format(@"{Products:list:{ {Product:{ItemCode}} }|,}", new { Products = products.Select(x=> (object)x) }); I am just getting started to see if I can use SmartFormat but seem not to be able to get it to do what I want. Best regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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);
/*
[
{
"Id:"{"SKU":"0001"}
},
{
"Id:"{"SKU":"0002"}
}
]
*/
} The better way could look like this, using the var result = JsonConvert.SerializeObject(arrayOfProducts);
/*
[{
"Id":{"SKU":"0001"},
"Name":"Item 1",
"UnitPrice":100
},
{
"Id":{"SKU":"0002"},
"Name":"Item 2",
"UnitPrice":110
}]
*/ |
Beta Was this translation helpful? Give feedback.
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: