如何使用json.net在C#中创建JSON数组



如何在C#中创建JSON,以便我们可以显示为阵列到HighChart。

[ 
   { y : 3, myData : 'firstPoint' },
   { y : 7, myData : 'secondPoint' },
   { y : 1, myData : 'thirdPoint' } 
]

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

您去这里:

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

相关内容

  • 没有找到相关文章

最新更新