dsb53973 2011-04-25 15:04
浏览 162

将JSON值转换为HTML表

I have a table.html, data.php and json.csv within the same folder.

data.php is doing fopen("json.csv","r") to read from json.csv.

How can I display the JSON objects as a table within table.html?

<html>
<head>
<script type="text/javascript" src="jquery.js">
function display(){
 $.post('data.php',function(data){
 $("#txtJSON").html(data); 
 });
  }

  </script>
  </head>
  <body onload="display()">
  <table id="#jobtable">
  <input type="text" id="txtJSON" />
  </table>
  </body>
  </html>
  • 写回答

3条回答 默认 最新

  • draxq02664 2011-04-25 15:13
    关注

    You just need json_decode and some iteration

    http://php.net/manual/en/function.json-decode.php

    I'll show you a very simplistic way to do the PHP. You don't tell anything about the JSON, so I'm assuming its flat (not nested). I think you might want to alter the client side stuff too. Make txtJSON into an empty div, which will be filled with the output from the PHP.

    $file = $fopen("json.csv", 'r');
    $fileText = fread($file, filesize($file));
    $arr = json_decode($fileText, true); //i prefer associative array in this context
    
    echo "<table>";
    foreach($arr as $k=>$v)
        echo "<tr><td>$k</td><td>$v</td></tr>";
    echo "</table>";
    

    If your JSON is not flat and simple, what would you like the resulting table to look like?

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大