using System.Collections.Generic;
using System.Data;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace MatchGroupSystem.Common
{
public class JsonMethods
{
public static string datatableToJSON(DataTable dt)
{
StringBuilder jsonStringBuilder = new StringBuilder();
StringWriter jsonStringWriter = new StringWriter(jsonStringBuilder);
JsonWriter jsonWriter = new JsonTextWriter(jsonStringWriter);
if (dt != null && dt.Rows.Count > 0)
{
jsonWriter.Formatting = Newtonsoft.Json.Formatting.None;
jsonWriter.WriteStartArray();
for (int i = 0; i < dt.Rows.Count; i++)
{
jsonWriter.WriteStartObject();
for (int j = 0; j < dt.Columns.Count; j++)
{
jsonWriter.WritePropertyName(dt.Columns[j].ColumnName.ToString());
jsonWriter.WriteValue(dt.Rows[i][j].ToString());
}
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
return jsonStringBuilder.ToString();
}
else
{
return null;
}
}
}
}