This article describes how to convert HTML table data to Json format. The following is a good example.
Script
Var keysArr = new Array ("key0", "key1", "key2 ");
Function TableToJson (tableid) {// tableid is the name of the table you want to convert. It is a string, such as "example"
Var rows = document. getElementById (tableid). rows. length; // obtain the number of rows (including thead)
Var colums = document. getElementById (tableid). rows [0]. cells. length; // obtain 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 for Json data
Json + = "\" "; // Add a double quotation mark
Json + = tdName;
Json + = "\"";
Json + = ":";
TdValue = document. getElementById (tableid). rows [I]. cells [j]. innerHTML; // Json data value
If (j = 1) {// column 1st is in the 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
The javascript Functions for converting table data to Json format are as follows: The Code is as follows: