Created
October 24, 2013 15:38
-
-
Save AugustoPedraza/7139472 to your computer and use it in GitHub Desktop.
Answer to [stackoverflow question](http://stackoverflow.com/questions/19567990/how-to-pass-c-sharp-datatable-to-a-javascript-function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
namespace GetJsonCollectionFromDataTable | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
var indexOfColumnNames = new Dictionary<int, string>(); | |
indexOfColumnNames.Add(5, "title"); | |
indexOfColumnNames.Add(9, "lat"); | |
indexOfColumnNames.Add(10, "lng"); | |
indexOfColumnNames.Add(11, "description"); | |
var columnsAreNotStringType = new List<string>() { "lat", "lng" }; | |
var length = 4; //Get the first 'n' elements | |
var dummyDataTable = CreateDummyTable(); | |
var jsonCollection = CreateJsonCollection(dummyDataTable, indexOfColumnNames, columnsAreNotStringType, length); | |
Console.WriteLine(jsonCollection); | |
Console.ReadLine(); | |
} | |
private static string CreateJsonCollection(DataTable dataTable, Dictionary<int, string> indexAndColumnNames, List<string> columnsAreNotStringType, int length) | |
{ | |
var preJsonCollection = new List<string>(); | |
for (int rowIndex = 0; rowIndex < length; rowIndex++) | |
preJsonCollection.Add(CreateJsonObjectFromRow(dataTable.Rows[rowIndex], indexAndColumnNames, columnsAreNotStringType)); | |
return string.Format("[{0}]", string.Join(",", preJsonCollection)); | |
} | |
private static DataTable CreateDummyTable() | |
{ | |
var dummyDataTable = new DataTable(); | |
for (int i = 0; i < 12; i++) | |
dummyDataTable.Columns.Add(new DataColumn()); | |
var dummyDataRow1 = dummyDataTable.NewRow(); | |
dummyDataRow1[5] = "via Andria, Roma"; | |
dummyDataRow1[9] = 41.8902643; | |
dummyDataRow1[10] = 12.6638589; | |
dummyDataRow1[11] = "fdsad"; | |
var dummyDataRow2 = dummyDataTable.NewRow(); | |
dummyDataRow2[5] = "via canosa, Roma"; | |
dummyDataRow2[9] = 41.8838417; | |
dummyDataRow2[10] = 12.5438227; | |
dummyDataRow2[11] = "fdsad"; | |
var dummyDataRow3 = dummyDataTable.NewRow(); | |
dummyDataRow3[5] = "via taranto, Milano"; | |
dummyDataRow3[9] = 45.4383343; | |
dummyDataRow3[10] = 9.1505354; | |
dummyDataRow3[11] = "fdsad"; | |
var dummyDataRow4 = dummyDataTable.NewRow(); | |
dummyDataRow4[5] = "via barletta, Roma"; | |
dummyDataRow4[9] = 41.9102707; | |
dummyDataRow4[10] = 12.4580826; | |
dummyDataRow4[11] = "fdsad"; | |
dummyDataTable.Rows.Add(dummyDataRow1); | |
dummyDataTable.Rows.Add(dummyDataRow2); | |
dummyDataTable.Rows.Add(dummyDataRow3); | |
dummyDataTable.Rows.Add(dummyDataRow4); | |
return dummyDataTable; | |
} | |
private static string CreateJsonObjectFromRow(DataRow dataRow, Dictionary<int, string> indexAndColumnNames, List<string> columnsAreNotStringType) | |
{ | |
var tmpPreJsonObject = new List<string>(indexAndColumnNames.Count); | |
foreach (var indexNamePair in indexAndColumnNames) | |
{ | |
var columnName = indexNamePair.Value; | |
var columnIndex = indexNamePair.Key; | |
var formatedString = columnsAreNotStringType.Contains(columnName) ? "\"{0}\" : {1}" : "\"{0}\" : \"{1}\""; | |
tmpPreJsonObject.Add(string.Format(formatedString, columnName, dataRow[columnIndex])); | |
} | |
return string.Format("{{{0}}}", string.Join(",", tmpPreJsonObject)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment