Example code for converting html table data to Json format

Example code for converting html table data to Json format
The javascript function for converting <table> table data to Json format is as follows

Copy code
The code is as follows:

<script>
var keysArr = new Array("key0", "key1", "key2");
function TableToJson(tableid) { //tableid is the name of the table you want to convert, a string, such as "example"
var rows = document.getElementById(tableid).rows.length; //Get the number of rows (including thead)
var colums = document.getElementById(tableid).rows[0].cells.length; //Get the number of columns
var json = "[";
var tdValue;
for (var i = 1; i < rows; i++) { //each row
json += "{";
for (var j = 0; j < colums; j++) {
tdName = keysArr[j]; //Key of Json data
json += "\""; //Add a double quote
json += tdName;
json += "\"";
json += ":";
tdValue = document.getElementById(tableid).rows[i].cells[j].innerHTML; //The value of the Json data
if (j === 1) {//The first column is in date format and needs to be added as follows according to JSON requirements
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>

<<:  Vue network request scheme native network request and js network request library

>>:  Recommended tips for web front-end engineers

Recommend

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

Vue component library ElementUI implements table loading tree data tutorial

ElementUI implements a table tree list loading tu...

How to obtain root permissions in a docker container

First, your container must be running You can vie...

Web front-end development course What are the web front-end development tools

With the development of Internet technology, user...

MySQL max_allowed_packet setting

max_allowed_packet is a parameter in MySQL that i...

How to disable web page styles using Firefox's web developer

Prerequisite: The web developer plugin has been in...

Reasons and solutions for MySQL sql_mode modification not taking effect

Table of contents Preface Scenario simulation Sum...

How to shrink the log file in MYSQL SERVER

The transaction log records the operations on the...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...

CSS makes tips boxes, bubble boxes, and triangles

Sometimes our pages will need some prompt boxes o...

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...

Table related arrangement and Javascript operation table, tr, td

Table property settings that work well: Copy code ...

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...