This questions is a little old but here's an example. If you have SQL Server 2016 you can use a lot of built-in json functions SQL Server Json Support
Google newtonsoft json for examples (or whatever library you're using). I'm using the Newtonsoft.Json
library. Make sure it's referenced in your project or add it via Nuget.
Your class should have a reference to it:
using Newtonsoft.Json;
Converting C# list of ChartModel
into json. The model (any POCO works) is defined as:
public class ChartModel
{
public string ChartType { get; set; }
public IList<ChartSeries> Data { get; set; }
public string ChartTitle { get; set; }
public int DisplayOrder { get; set; }
}
Convert List
to its json version:
// Charts = List<ChartModel>
var chartJson = JsonConvert.SerializeObject(Charts);
// or if you want the json formatted
var chartJson = JsonConvert.SerializeObject(products, Formatting.Indented);
Now you have your json, a string, that can be stored in any nvarchar
defined column.