表數據轉Json 格式的javascript函數如下
<script> <br />var keysArr = new Array("key0", "key1","key2"); <br />function TableToJson(tableid) { //tableid是你要轉換的表的表名,是一個字串,如"example" <br />var rows = document.getElementById(tableid).rows.length; //獲得行數(包括thead) <br />var colums = document.getElementById(tableid).rows[ 0].cells.length; //取得列數<br />var json = "["; <br />var tdValue; <br />for (var i = 1; i < rows; i ) { //每行<br />json = "{"; <br />for (var j = 0; j < colums; j ) { <br />tdName = keysArr[j]; //Json資料的鍵<br />json = """; //加上一個雙引號<br />json = tdName; <br />json = """; <br />json = ":"; <br />tdValue = document.getElementById(tableid).rows[i].cells [j].innerHTML;//Json資料的值<br />if (j === 1) {//第1列是日期格式,需要按照json要求做如下加<br />tdValue = "/Date(" tdValue ")/"; <br />} <br />json = """; <br />json = tdValue; <br />json = """; <br />json = ","; <br />} <br />json = json.substring(0, json.length - 1); <br />json = "}"; <br />json = ","; <br />} <br />json = json.substring(0, json .length - 1); <br />json = "]"; <br />return json; <br />} <br /></script>