Home  >  Article  >  Web Front-end  >  How to convert table data in html to Json format

How to convert table data in html to Json format

php中世界最好的语言
php中世界最好的语言Original
2018-02-08 09:42:514418browse

This time I will show you how to convert table data in html to Json format. What are the things to pay attention to when converting table table data in html to Json format? The following is a practical case. Let’s take a look. . f5d188ed2c074f8b944552db028f98a1The

javascript

function to convert table data to Json format is as follows

<script> 
var keysArr = new Array("key0", "key1","key2"); 
function TableToJson(tableid) { //tableid是你要转化的表的表名,是一个字符串,如"example" 
var rows = document.getElementById(tableid).rows.length; //获得行数(包括thead) 
var colums = document.getElementById(tableid).rows[0].cells.length; //获得列数 
var json = "["; 
var tdValue; 
for (var i = 1; i < rows; i++) { //每行 
json += "{"; 
for (var j = 0; j < colums; j++) { 
tdName = keysArr[j]; //Json数据的键 
json += "\""; //加上一个双引号 
json += tdName; 
json += "\""; 
json += ":"; 
tdValue = document.getElementById(tableid).rows[i].cells[j].innerHTML;//Json数据的值 
if (j === 1) {//第1列是日期格式,需要按照json要求做如下添加 
tdValue = "\/Date(" + tdValue + ")\/"; 
} 
json += "\""; 
json += tdValue; 
json += "\""; 
json += ","; 
} 
json = json.substring(0, json.length - 1); 
json += "}"; 
json += ","; 
} 
json = json.substring(0, json.length - 1); 
json += "]"; 
return json; 
} 
</script>

I believe you have mastered it after reading these cases Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

How to redirect the connection in html


##What is the difference between html, xhtml and xml

The above is the detailed content of How to convert table data in html to Json format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn