Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert table data into Json format in html

Shulou Source: shulou.com Published: 2022-06-01 10:36:48 11月02日 Update

This article mainly introduces the relevant knowledge of "how to convert table table data into Json format". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to convert table table data into Json format in htmltable" can help you solve the problem.

Var keysArr = new Array ("key0", "key1", "key2")

Function TableToJson (tableid) {/ / tableid is the table name of the table you want to convert, which is 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; iTunes +) {/ / per line

Json + = "{"

For (var j = 0; j < colums; jacks +) {

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] [xss_clean]; / / the value of 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

}

This is the end of the introduction about "how to convert the table data in html to Json format". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Data format knowledge industry different practical content characters strings practicality actual quotation marks articles methods dates more cases knowledge points articles information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Microsoft vpn OPPO Reno