我正在尝试创建以下 JSON 数据:
{
'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'
]
}
我正在使用 C# 并尝试创建一个对象以执行此操作.....类似于:
public class chart{
public string[] chart.labels {get;set;}
public string[] chart.tooltips {get;set;}
}
但显然我不能有包含空格的属性。
我该怎么做呢?
更新:
使用 JamieC 的回答可以完美地完成以下工作
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; }
}
}
产生:
{"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"]}