Hi DanielFran,
You need to use List with Dictionary and JavaScriptSerializer to return your datatable data, you are getting ftpserver as datatable add this datatable records in list dictionary.
Please refer below sample.
Namespaces
C#
using System.Data;
using System.Web.Script.Serialization;
using System.IO;
VB.Net
Imports System.Data
Imports System.Web.Script.Serialization
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(string)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
Dictionary<string, object> childRow = new Dictionary<string, object>();
foreach (DataRow dr in dt.Rows)
{
childRow = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
childRow.Add(dc.ColumnName, dr[dc]);
}
parentRow.Add(childRow);
}
string json = serializer.Serialize(parentRow);
File.WriteAllText(Server.MapPath("json.txt"), json.ToString());
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(String)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
Dim parentRow As List(Of Dictionary(Of String, Object)) = New List(Of Dictionary(Of String, Object))()
Dim childRow As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
For Each dr As DataRow In dt.Rows
childRow = New Dictionary(Of String, Object)()
For Each dc As DataColumn In dt.Columns
childRow.Add(dc.ColumnName, dr(dc))
Next
parentRow.Add(childRow)
Next
Dim json As String = serializer.Serialize(parentRow)
File.WriteAllText(Server.MapPath("json.txt"), json.ToString())
End If
End Sub
Output
[{"Id":"1","Name":"John Hammond","Country":"United States"},
{"Id":"2","Name":"Mudassar Khan","Country":"India"},
{"Id":"3","Name":"Suzanne Mathews","Country":"France"},
{"Id":"4","Name":"Robert Schidner","Country":"Russia"}]