sushil
sushil

Reputation: 321

how to create JSON array in C# using JSON.NET

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

Answers (2)

hasanaydogar
hasanaydogar

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

stefankmitph
stefankmitph

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

Related Questions