Share |In this post I will discuss about converting any data table in the JSON array. JSON can be very effectively used for Ajax operations since data handling becomes more easier in case of JSON. Here I will not discuss how to use JSON in Ajax. I will simply provide code for converting Data Table into JSON Array.
Remember to add System.Web.Script.Serialization and System.Globalization; namespaces
Let us assume a demo table as below (DOWNLOAD)
For this table JSON output will be as below
Add following functions to build JSON string from an object
Above functions will be responsible to convert an object into JSON variable, now we will write one function which will break the table and call above functions, which in turn update the string builder.
In page load we will simply call following code, this will convert table into JSON string and write it on Page.
In above code I have registered JAVA script block as well, this just for demo purpose; if we put following code we will get an alert with first employee ID.
Download Demo Code Here --> DataTableToJSONArray.zip