// Install Newtonsoft.Json from nuget
//then add namespace
//Using NewtonSoft.Json;
public bool Write(string filePath, DataTable table, string fileName)
{
try
{
bool success = false;
using (var file = File.CreateText(filePath + "" + fileName))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, table);
success = true;
}
return success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
// Install Newtonsoft.Json from nuget
// Use the namespace after
//Using Newtonsoft.Json;
public DataTable Read(string filePath)
{
try
{
//check if json structure is okay (http://jsonlint.com)
//generate object class http://www.jsonutils.com
DataTable dt = new DataTable();
dt = JsonConvert.DeserializeObject<DataTable>(File.ReadAllText(filePath));
return dt;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}