将json解析的值插入表中

时间:2013-04-26 09:12:06

标签: php javascript html

helo我已经努力设计了在页面中打印randamoly的json值,这里有4个类别,我用javascript做的是找到包含许多对象的json数组的长度,然后取类型id,因为它在所有数组中很常见,要确定它们是否属于0,1,2,3类,然后打印json数组中的所有元素,依此类推,这里我想为每个类别创建表并推送所有对应于表行的id。

var txt = '<?php echo $response ?>';
//alert(txt);
//var jsonData = eval ("(" + txt + ")");
var jsonData = JSON.parse(txt);
for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
document.write("<table border="0" width="500>">")
if(counter.type=="0")
    {

      document.write(counter.building_name);
      document.write(counter.org_name);
      document.write(counter.user_name);
      document.write(counter.name);
      document.write(counter.loc_name);
      document.write(counter.email_id);
      document.write(counter.password);

    }
if(counter.type=="1")
    {
        document.write(counter.user_name);
        document.write(counter.name);
        document.write(counter.password);
        document.write(counter.email_id);
    }
    if(counter.type=="2")
        {
            document.write(counter.building_name);
            document.write(counter.org_name);
            document.write(counter.user_name);
            document.write(counter.opr_code);
            document.write(counter.name);
            document.write(counter.loc_name);
            document.write(counter.email_id);
            document.write(counter.password);
        }
        if(counter.type=="3")
            {
               document.write(counter.building_name);
               document.write(counter.org_name);
               document.write(counter.machine_type);
               document.write(counter.activate_status);
               document.write(counter.machine_name);
               document.write(counter.entrance_exit_name);
               document.write(counter.entrance_or_exit);
               document.write(counter.loc_name);
               document.write(counter.activation_code);
            }
}
document.write("</table>")

1 个答案:

答案 0 :(得分:0)

试试这个:

 for (var i = 0; i < jsonData.account_detail.length; i++) {
var counter = jsonData.account_detail[i];
//console.log(counter.counter_name);
//alert(counter.type);
var tbl_str = '<table border="0" width="500><tr>';
    if(counter.type=="0")
    {
      tbl_str += "<td>"+counter.building_name+"</td>";
      tbl_str += "<td>"+counter.org_name+"</td>";
      tbl_str += "<td>"+counter.user_name+"</td>";
      tbl_str += "<td>"+counter.name+"</td>";
      tbl_str += "<td>"+counter.loc_name+"</td>";
      tbl_str += "<td>"+counter.email_id+"</td>";
      tbl_str += "<td>"+counter.password+"</td>";
    }
    else if(counter.type=="1")
    {
        tbl_str += "<td>"+counter.user_name+"</td>";
        tbl_str += "<td>"+counter.name+"</td>";
        tbl_str += "<td>"+counter.password+"</td>";
        tbl_str += "<td>"+counter.email_id+"</td>";
    }
    else if(counter.type=="2")
    {
            tbl_str += "<td>"+counter.building_name+"</td>";
            tbl_str += "<td>"+counter.org_name+"</td>";
            tbl_str += "<td>"+counter.user_name+"</td>";
            tbl_str += "<td>"+counter.opr_code+"</td>";
            tbl_str += "<td>"+counter.name+"</td>";
            tbl_str += "<td>"+counter.loc_name+"</td>";
            tbl_str += "<td>"+counter.email_id+"</td>";
            tbl_str += "<td>"+counter.password+"</td>";
     }
     else {
               tbl_str += "<td>"+counter.building_name+"</td>";
               tbl_str += "<td>"+counter.org_name+"</td>";
               tbl_str += "<td>"+counter.machine_type+"</td>";
               tbl_str += "<td>"+counter.activate_status+"</td>";
               tbl_str += "<td>"+counter.machine_name+"</td>";
               tbl_str += "<td>"+counter.entrance_exit_name+"</td>";
               tbl_str += "<td>"+counter.entrance_or_exit+"</td>";
               tbl_str += "<td>"+counter.loc_name+"</td>";
               tbl_str += "<td>"+counter.activation_code+"</td>";
            }    
tbl_str += "</tr></table>";    
document.write(tbl_str);    
}