Skip to content Skip to sidebar Skip to footer

Html Table To Json From Another Url

Hey guys i am using this lib to convert html table to json array. http://lightswitch05.github.io/table-to-json/ With this my table is transformed into many objects and it shows it

Solution 1:

You can use Range.createContextualFragment to turn the data into an DocumentFragment.

Then, you can use querySelector to find your table, and get it with jQuery to call tableToJSON.

var frag = document.createRange().createContextualFragment(data);
$(frag.querySelector('table')).tableToJSON();

UPDATE

Since it's not working, probably it's because tableToJSON requires that the element is placed inside the document to work. So, you can do something like that:

var frag = document.createRange().createContextualFragment(data);
var tbl = frag.querySelector('table');
document.body.appendChild(tbl);
var myjson = $(tbl).tableToJSON();
tbl.parentNode.removeChild(tbl);

That way, you append the table to the document temporarly only to get the JSON, and immediately remove it as soon as the json was generated. Leave a comment if that solution doesn't work either.

Post a Comment for "Html Table To Json From Another Url"