JSON data processing.
using System.Web.Mvc;
using Newtonsoft.Json;
using ProjectC.Core;
namespace ProjectC.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult CalculateData()
{
var dataPath = Server.MapPath("~/App_Data/data.json");
if (System.IO.File.Exists(dataPath))
{
var json = System.IO.File.ReadAllText(dataPath);
var dataProcessor = new DataProcessor();
return dataProcessor.IsJsonValid(json)
? Json("{\"result\": \"success\", \"data\": " + JsonConvert.SerializeObject(dataProcessor.ProcessData(json)) + "}")
: Json("{\"result\": \"error\", \"data\": \"File '" + dataPath.Replace(@"\", @"\\") + "' is not valid JSON.\" }");
}
return Json("{\"result\": \"error\", \"data\": \"File '" + dataPath.Replace(@"\", @"\\") + "' not found.\" }");
}
}
}