You could get JSON file data in an action and call this action using fetch/ajax in javascript.
Refer to my demo where myData.json is located in the root of project and use HomeController as an example.
1.HomeController.cs
public class HomeController : Controller
{
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult GetJsonValue()
{
string contentRootPath = _hostingEnvironment.ContentRootPath;
string path = Path.Combine(contentRootPath, "myData.json");
var JSON = System.IO.File.ReadAllText(path);
var jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(JSON);
return new JsonResult(jsonObj);
}
}
2.Javascript
//use ajax
$.ajax({
type:"GET",
url: "/Home/GetJsonValue"
}).done(
function(data){
console.log(data);
});
//or use fetch
fetch("/Home/GetJsonValue")
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
});