window.onload = function () {
var datanav = [{ "name": "张三", "position": "1", "powerrate": "2", "waterrate": "3", "healthcosts": "4", "watermeter": "5", "ammeter": "6", "linkman": "7", "phone": "8", "rental": "9", "tenancy": "10", "remark": "11" }, { "name": "李四", "position": "11", "powerrate": "10", "waterrate": "9", "healthcosts": "8", "watermeter": "7", "ammeter": "6", "linkman": "5", "phone": "4", "rental": "3", "tenancy": "2", "remark": "1"}];
var tb = document.getElementById("tb");
for (var i = 0; i < datanav.length; i++) {
var row = tb.insertRow(tb.rows.length);
var c1 = row.insertCell(0);
c1.innerHTML = datanav[i].name;
var c2 = row.insertCell(1);
c2.innerHTML = datanav[i].waterrate;
var c3 = row.insertCell(2);
c3.innerHTML = datanav[i].powerrate;
var c4 = row.insertCell(3);
c4.innerHTML = datanav[i].healthcosts;
var c5 = row.insertCell(4);
c5.innerHTML = datanav[i].watermeter;
var c6 = row.insertCell(5);
c6.innerHTML = datanav[i].ammeter;
var c7 = row.insertCell(6);
c7.innerHTML = datanav[i].linkman;
var c8 = row.insertCell(7);
c8.innerHTML = datanav[i].phone;
var c9 = row.insertCell(8);
c9.innerHTML = datanav[i].rental;
var c10 = row.insertCell(9);
c10.innerHTML = datanav[i].tenancy;
}
}
租户 | 水费 | 电费 | 卫生费 | 水表位置 | 电表位置 | 联系人 | 电话 | 房费 | 租赁合同期限 |
首先要把JSON类型的字符转换成数组。这就要引用json.js 的一个包了。
我上传了附件。在你的页面引用这个。
然后转换即可,
var arry = JSON.parse(datanav);
你看一下 Json.js 里还有个方法是数组或对象转换成Json 格式的字符串
JSON.stringify(param);
js可以写成:
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}];
//遍历
for(var o in data){
alert(o);
alert(data[o]);
alert("text:"+data[o].name+" value:"+data[o].age );
}
或是
function text(){
var json = {"options":"[{"text":"王家湾","value":"9"},{"text":"李家湾","value":"10"},{"text":"邵家湾","value":"13"}]"}
json = eval(json.options)
for(var i=0; ialert(json[i].text+" " + json[i].value)
}
}