Hi friends,
i having html table in stored in a variable in jquery.. i need to convert that table to json... how?
example:
var table=<table id="jsontable">....</table>
how to convert above table to json text
Hi sureshMGR,
Check the below example.
HTML
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> window.onload = function () { var table = '<table id="jsontable"><tr><th>Id</th><th>Name</th><th>Country</th></tr>' + '<tr><td>1</td><td>John Hammond</td><td>United States</td></tr>' + '<tr><td>2</td><td>Mudassar Khan</td><td>India</td></tr>' + '<tr><td>3</td><td>Suzanne Mathews</td><td>France</td></tr></table>'; var jsonString = { Rows: [] }; var $th = $(table).find('th'); $(table).find('tbody tr').each(function (i, tr) { if (i > 0) { var obj = {}; $tds = $(tr).find('td'); $th.each(function (index, th) { obj[$(th).text()] = $tds.eq(index).text(); }); jsonString.Rows.push(obj); } }); alert(JSON.stringify(jsonString)); } </script> </head> <body> </body> </html>
Demo
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.