/*
/*功能:返回表tabView的所有数据,以xml的格式返回
*/
function returnTableJson() {
var sTable = document.getElementById("tabView");
var arr = [];
var obj = {};
for (var i = 1; i < sTable.rows.length; i++) {
obj.index = sTable.rows[i].cells[0].innerHTML;
obj.name = sTable.rows[i].cells[1].innerHTML;
obj.email = sTable.rows[i].cells[2].innerHTML;
obj.info = sTable.rows[i].cells[3].innerHTML;
arr.push(obj);
obj = {};
}
//这里将js对象转为json对象
var strJson = JSON.stringify(arr);
var obj2 = eval(strJson);
var xml = "";
for (var j = 0; j < obj2.length; j++) {
xml += "<user>" + "<name>" + obj2[j].name + "</name>" + "<email>" + obj2[j].email + "</email>" + "<info>" + obj2[j].info + "</info></user>"
}
return encodeURIComponent(xml);
}
注:对json对象的遍历使用的循环是:
var json = eval(data);
var tab = document.getElementById('tabView');
for( i in json ){
var insertRow = tab.insertRow();
insertRow.className = "idInsertRow";
index = insertRow.rowIndex;
insertRow.onclick = onSelectRow;
var nCellNo = insertRow.insertCell(0);
var sCellName = insertRow.insertCell(1);
var sCellEmail = insertRow.insertCell(2);
var sCellInfo = insertRow.insertCell(3);
nCellNo.innerHTML = insertRow.rowIndex;
sCellName.innerHTML = json[i].name;
sCellEmail.innerHTML = json[i].email;
sCellInfo.innerHTML = json[i].info;
}
//将json对象转换为js对象
var jsontext =
'{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';
var contact = JSON.parse(jsontext);