0

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' } 
]

2 Answers 2

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

4

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);

2 Comments

I want to use like list.Add( new Data() { y = 3, myData = "firstPoint"}); but it giving me error
y and myData not in context

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.