如何在 C# 中创建 JSON,以便我们可以将传递作为数组显示到 highchart。
[
{ y : 3, myData : 'firstPoint' },
{ y : 7, myData : 'secondPoint' },
{ y : 1, myData : 'thirdPoint' }
]
Run Code Online (Sandbox Code Playgroud)
干得好:
class Data
{
public int y { get; set; }
public string myData { get; set; }
}
List<Data> list = new List<Data>
{
new Data() { y = 3, myData = "firstPoint"},
new Data() { y = 7, myData = "secondPoint"},
new Data() { y = 1, myData = "thirdPoint"},
};
string json = JsonConvert.SerializeObject(list);
Run Code Online (Sandbox Code Playgroud)
在MVC 中,您可以使用 JsonResult 类。
public ActionResult Movies()
{
var movies = new List<object>();
movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });
return Json(movies, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)