Sample Code _ HTML/Xhtml _ for converting HTML table data to Json format

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.