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

MySQL Database Indexes and Transactions

Table of contents 1. Index 1.1 Concept 1.2 Functi...

IIS7 IIS8 reverse proxy rule writing, installation and configuration method

Purpose: Treat Station A as the secondary directo...

How much do you know about JavaScript inheritance?

Table of contents Preface The relationship betwee...

Detailed explanation of application scenarios of filters in Vue

filter is generally used to filter certain values...

MySql knowledge points: transaction, index, lock principle and usage analysis

This article uses examples to explain the princip...

How to install PostgreSQL and PostGIS using yum on CentOS7

1. Update the yum source The PostgreSQL version o...

How to notify users of crontab execution results by email

symptom I set a crontab task on a centos7 host, b...

Introduction to the B-Tree Insertion Process

In the previous article https://www.jb51.net/arti...

Windows platform configuration 5.7 version + MySQL database service

Includes the process of initializing the root use...

Detailed description of ffmpeg Chinese parameters

FFMPEG 3.4.1 version parameter details Usage: ffm...

A collection of possible problems when migrating sqlite3 to mysql

Brief description Suitable for readers: Mobile de...

Beginners understand MySQL deadlock problem from source code

After many difficult single-step debugging late a...

Using CSS3 to achieve transition and animation effects

Why should we use CSS animation to replace JS ani...