When you go in to a particular url, you will be pointed to a view (most of the time).
With Newtonsoft's JSON.NET, those urls can be used to retrieve JSON.
Before we start, you should create a simple MVC application, in that application,
right click and go to the nuget package manager (right click -> manage nuget packages).
in there search for
Newtonsoft.Json
install it to your application.
in order for us to use the JSON.NET, we have to import the following in to our code (here I am putting them in the controller)
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
now that we have the relevant imports, lets see some sample code to return json,
use the following code to make the relevant JSON comply with the camel case notation
var camelCaseFormatter = new JsonSerializerSettings();
camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
now lets create a ContentResult object that we can return.
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(
<<this will have the returning object>>
,
camelCaseFormatter
),
ContentType = "application/json"
};
return jsonResult;
Heres a complete example.
in models we can create,
public class EmployeeDisplayModel
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
in the controller add the following action
public class HomeController : Controller
{
public ActionResult GetAllEmployeesJSON()
{
List<EmployeeDisplayModel> empDisplayList = new List<EmployeeDisplayModel>();
empDisplayList.Add(new EmployeeDisplayModel() {
EmployeeID = 1,
EmployeeName = "emp1"
});
empDisplayList.Add(new EmployeeDisplayModel()
{
EmployeeID = 2,
EmployeeName = "emp2"
});
empDisplayList.Add(new EmployeeDisplayModel()
{
EmployeeID = 3,
EmployeeName = "emp3"
});
var camelCaseFormatter = new JsonSerializerSettings();
camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(
empDisplayList
,
camelCaseFormatter
),
ContentType = "application/json"
};
return jsonResult;
}
}
when you can access them by going to the relavent /<<controller>>/<<action>> link,
Heres a complete example.
in models we can create,
public class EmployeeDisplayModel
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
}
in the controller add the following action
public class HomeController : Controller
{
public ActionResult GetAllEmployeesJSON()
{
List<EmployeeDisplayModel> empDisplayList = new List<EmployeeDisplayModel>();
empDisplayList.Add(new EmployeeDisplayModel() {
EmployeeID = 1,
EmployeeName = "emp1"
});
empDisplayList.Add(new EmployeeDisplayModel()
{
EmployeeID = 2,
EmployeeName = "emp2"
});
empDisplayList.Add(new EmployeeDisplayModel()
{
EmployeeID = 3,
EmployeeName = "emp3"
});
var camelCaseFormatter = new JsonSerializerSettings();
camelCaseFormatter.ContractResolver = new CamelCasePropertyNamesContractResolver();
var jsonResult = new ContentResult
{
Content = JsonConvert.SerializeObject(
empDisplayList
,
camelCaseFormatter
),
ContentType = "application/json"
};
return jsonResult;
}
}
when you can access them by going to the relavent /<<controller>>/<<action>> link,
No comments:
Post a Comment