I have the following:
var data = new List<DataModel>();
Where DataModel is the following:
public class DataModel {
public DateTime Date { get; set; }
public Int32 Users { get; set; }
}
How turn this List into a JSON forma and return it in a WebAPI 2.0 action?
Thank you, Miguel
You can do it the magic way...
public class JsonListObjectController : ApiController
{
public List<DataModel> Get()
{
var data = new List<DataModel>()
{
new DataModel() {Date = DateTime.Today, Users = 100},
new DataModel() {Date = DateTime.Today, Users = 120}
};
return data;
}
}
or you can do it the "I want to stay in control way"
public HttpResponseMessage Get()
{
var data = new List<DataModel>()
{
new DataModel() {Date = DateTime.Today, Users = 100},
new DataModel() {Date = DateTime.Today, Users = 120}
};
return new HttpResponseMessage()
{
Content = new StringContent(JArray.FromObject(data).ToString(), Encoding.UTF8, "application/json")
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With