将HTML表格转换为另一个URL中的JSON [英] HTML Table to JSON from another URL

查看:83
本文介绍了将HTML表格转换为另一个URL中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 lib html 表转换为 json数组



http://lightswitch05.github.io/table-to-json/



通过这个我的表格被转换成了许多对象,并将它显示在chrome控制台。



当我和表格在同一页面上时,它工作正常,但我想要获取 json 到另一页上。最初,我做到了这一点,目标是我的元素,并得到它转换,但没有运气有任何想法?

  $。get(http ://support.jonar.com/support/default.asp?W2305,函数(数据){
var table_csv = $(table.testtable)。html(data); //这给了我整个html doc
console.log(table_csv);
var myjson = $(table_csv).tableToJSON();
});

这将返回整个文档.. for table_csv 。当我将html < table>< / table> 存储到一个变量中,并且我运行 $(table_csv).tableToJSON(); 我得到一个空值,我只看到 []



谢谢你们!

解决方案

您可以使用 Range.createContextualFragment 将数据转换为 DocumentFragment

然后,您可以使用 querySelector 来查找你的表,并用jQuery来调用 tableToJSON

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






更新 p>

由于它不起作用,可能是因为 tableToJSON 要求元素放置在文档中才能工作。所以,你可以这样做:

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

通过这种方式,您可以临时将表追加到文档中以获取JSON,并立即将其删除只要json生成。如果该解决方案不起作用,请留言。


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 in the chrome console.

It works when i am on the same page as the table but i want to get the json onto another page. Initially i did this to target my element and get it to transform it but no luck any ideas?

$.get("http://support.jonar.com/support/default.asp?W2305", function(data) {
    var table_csv = $("table.testtable").html(data); //this gives me the entire html doc
    console.log(table_csv);
    var myjson = $(table_csv).tableToJSON();
});

This returns the whole document.. for table_csv. Also when i store the html <table></table> into a variable and i run $(table_csv).tableToJSON(); i get an empty value i only see []

Thanks guys!

解决方案

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.

这篇关于将HTML表格转换为另一个URL中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆