If you are developing a .Net Core WebApi or WebSite you dont not need to install newtownsoft.json to perform json serialization/deserealization
Just make sure that your controller method returns a JsonResult
and call return Json(<objectoToSerialize>);
like this example
namespace WebApi.Controllers
{
[Produces("application/json")]
[Route("api/Accounts")]
public class AccountsController : Controller
{
// GET: api/Transaction
[HttpGet]
public JsonResult Get()
{
List<Account> lstAccounts;
lstAccounts = AccountsFacade.GetAll();
return Json(lstAccounts);
}
}
}
If you are developing a .Net Framework WebApi or WebSite you need to use NuGet to download and install the newtonsoft json
package
"Project" -> "Manage NuGet packages" -> "Search for "newtonsoft json". -> click "install".
namespace WebApi.Controllers
{
[Produces("application/json")]
[Route("api/Accounts")]
public class AccountsController : Controller
{
// GET: api/Transaction
[HttpGet]
public JsonResult Get()
{
List<Account> lstAccounts;
lstAccounts = AccountsFacade.GetAll();
//This line is different !!
return new JsonConvert.SerializeObject(lstAccounts);
}
}
}
More details can be found here - https://learn.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-2.1