将JSON值转换为HTML表

use*_*079 4 html php json

我在同一个文件夹中有一个table.html,data.phpjson.csv.

data.php正在fopen("json.csv","r")json.csv读取.

如何在table.html中将 JSON对象显示为表格?

<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>
Run Code Online (Sandbox Code Playgroud)

jon*_*tar 9

你只需要json_decode和一些迭代

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

我将向您展示一种非常简单的PHP方法.你没有告诉任何关于JSON的事情,所以我假设它是扁平的(不是嵌套的).我想你可能也想改变客户端的东西.make txtJSON成一个空的div,将填充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>";
Run Code Online (Sandbox Code Playgroud)

如果您的JSON不简单,那么您希望结果表看起来像什么?