I am trying to create the following JSON data:
{
'chart.labels': ['Bob','Lucy','Gary','Hoolio'],
'chart.tooltips': ['Bob did well',
'Lucy had her best result',
'Gary - not so good',
'Hoolio had a good start'
]
}
I am using C# and trying to create an object in order to do this.....something like:
public class chart{
public string[] chart.labels {get;set;}
public string[] chart.tooltips {get;set;}
}
but obviously I cannot have properties containing spaces.
How would I go about doing this ?
UPDATE:
Using JamieC's answer the following works perfecly
public virtual ActionResult CompanyStatus()
{
var labelList = new List<string>() { "Bob", "Lucy", "Gary", "Hoolio" };
var tooltipsList = new List<string>() { "Bob did well", "Lucy had her best result", "Gary - not so good", "Hoolio had a good start" };
var cData = new chartData()
{
Labels = labelList.ToArray(),
Tooltips = tooltipsList.ToArray()
};
var serializer = new DataContractJsonSerializer(cData.GetType());
String output;
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, cData);
output = Encoding.Default.GetString(ms.ToArray());
}
return this.Content(output);
}
[DataContract]
public class chartData
{
[DataMember(Name = "chart.labels")]
public string[] Labels { get; set; }
[DataMember(Name = "chart.tooltips")]
public string[] Tooltips { get; set; }
}
}
Which produces:
{"chart.labels":["Bob","Lucy","Gary","Hoolio"],"chart.tooltips":["Bob did well","Lucy had her best result","Gary - not so good","Hoolio had a good start"]}