Reputation: 321
How to create JSON in C# so that we can display pass as array to highchart.
[
{ y : 3, myData : 'firstPoint' },
{ y : 7, myData : 'secondPoint' },
{ y : 1, myData : 'thirdPoint' }
]
Upvotes: 0
Views: 8548
Reputation: 905
In MVC You can use JsonResult class.
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);
}
Upvotes: 5
Reputation: 3306
Here you go:
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);
Upvotes: 4