Hi sanjay8090,
Check this example. Now please take its reference and correct your code.
HTML
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var arr = [["EmployeeId", "Company", "EmployeeName", "Location"],
[211, "inyxa", "Sanjay", "Faridabad"],
[235, "Tata", "Shashank mishra", "Noida"],
[123, "Kabir infosystem", "Surya singh", "Gaziabad"],
[444, "Velocis", "Ajay kumar", "Merath"],
[595, "W3wila", "Virendra shakya", "Kanpur"],
[256, "Tech Mahindra", "Ayush trivedi", "Banaras"]];
$("#tblEmployees").hide();
$('#btnSerializeDeserialize').on('click', function () {
$.ajax({
type: 'POST',
url: "Default.aspx/SerializeDeserialize",
data: "{'data':'" + JSON.stringify(arr) + "'}",
dataType: 'json',
contentType: "application/json; charset=utf-8"
}).success(function (response) {
var employees = JSON.parse(response.d);
if (employees.length > 0) {
$("#tblEmployees").show();
var row = $("#tblEmployees tr:last-child").clone(true);
$("#tblEmployees tr").not($("#tblEmployees tr:first-child")).remove();
$.each(employees, function () {
var employee = this;
$("td", row).eq(0).html(employee.EmployeeId);
$("td", row).eq(1).html(employee.EmployeeName);
$("td", row).eq(2).html(employee.Company);
$("td", row).eq(3).html(employee.Location);
$("#tblEmployees").append(row);
row = $("#tblEmployees tr:last-child").clone(true);
});
} else {
$("#tblEmployees").hide();
}
}).error(function (response) {
alert(response.d);
});
});
});
</script>
<input id="btnSerializeDeserialize" type="button" value="Serialize Deserialize" />
<hr />
<table id="tblEmployees">
<tr>
<th>Id</th>
<th>Name</th>
<th>Company</th>
<th>Location</th>
</tr>
<tr>
<td>Id</td>
<td>Name</td>
<td>Company</td>
<td>Location</td>
</tr>
</table>
Namespaces
C#
using System.Data;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
VB.Net
Imports System.Data
Imports System.Web.Services
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Code
C#
[WebMethod]
public static string SerializeDeserialize(string data)
{
DataTable dt = new DataTable();
// Deserialize the data to DataTable.
JArray arrays = (JArray)JsonConvert.DeserializeObject(data);
if (arrays.Count > 0)
{
foreach (JToken jToken in arrays[0])
{
dt.Columns.Add((jToken as JValue).Value.ToString());
}
int i = 0;
foreach (var array in arrays)
{
if (i > 0)
{
DataRow dr = dt.NewRow();
int j = 0;
foreach (JToken jToken in array)
{
dr[j] = (jToken as JValue).Value;
j++;
}
dt.Rows.Add(dr);
}
i++;
}
}
// Serialize the DataTable.
return JsonConvert.SerializeObject(dt);
}
VB.Net
<WebMethod()>
Public Shared Function SerializeDeserialize(ByVal data As String) As String
Dim dt As DataTable = New DataTable()
' Deserialize the data to DataTable.
Dim arrays As JArray = CType(JsonConvert.DeserializeObject(data), JArray)
If arrays.Count > 0 Then
For Each jToken As JToken In arrays(0)
dt.Columns.Add((TryCast(jToken, JValue)).Value.ToString())
Next
Dim i As Integer = 0
For Each array In arrays
If i > 0 Then
Dim dr As DataRow = dt.NewRow()
Dim j As Integer = 0
For Each jToken As JToken In array
dr(j) = (TryCast(jToken, JValue)).Value
j += 1
Next
dt.Rows.Add(dr)
End If
i += 1
Next
End If
' Serialize the DataTable.
Return JsonConvert.SerializeObject(dt)
End Function
Screenshot
