Home > Web Front-end > HTML Tutorial > Sample code for converting html table data to Json format_HTML/Xhtml_Web page production

Sample code for converting html table data to Json format_HTML/Xhtml_Web page production

WBOY
Release: 2016-05-16 16:39:04
Original
1289 people have browsed it

The javascript function to convert table data to Json format is as follows

Copy the code
The code is as follows:

<script> <br>var keysArr = new Array("key0", "key1","key2"); <br>function TableToJson(tableid) { //tableid is the table of the table you want to convert The name is a string, such as "example" <br>var rows = document.getElementById(tableid).rows.length; //Get the number of rows (including thead) <br>var colums = document.getElementById(tableid). rows[0].cells.length; //Get the number of columns <br>var json = "["; <br>var tdValue; <br>for (var i = 1; i < rows; i ) { // Each row<br />json = "{"; <br />for (var j = 0; j < colums; j ) { <br />tdName = keysArr[j]; //Keys of Json data<br />json = """; //Add a double quote <br />json = tdName; <br />json = """; <br />json = ":"; <br />tdValue = document.getElementById(tableid).rows[ i].cells[j].innerHTML;//The value of Json data<br />if (j === 1) {//The first column is the date format, you need to add it as follows according to the json requirements<br />tdValue = " /Date(" tdValue ")/"; <br />} <br />json = """; <br />json = tdValue; <br />json = """; <br />json = ","; <br />} <br />json = json.substring(0, json.length - 1); <br />json = "}"; <br />json = ","; <br />} <br />json = json.substring (0, json.length - 1); <br />json = "]"; <br />return json; <br />} <br /></script>
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template