如何呈現表中的JSON數據。我試着用下面的代碼:JSON jQuery顯示到表
HTML:
<table>
<thead>
<tr>
<th>id</th>
<th>Name</th>
<th>Password</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
<button id="get">Get</button>
<script src="script/ajax.googleapis.com_ajax_libs_jquery_1.10.1_jquery.min.js" type="text/javascript"></script>
<!--<script src="script/myScript.js" type="text/javascript"></script>-->
<script src="script/json_array.js" type="text/javascript"></script>
腳本的jQuery的
$(document).ready(function() {
done();
});
function done() {
setTimeout(function() {
updates();
done();
}, 200);
}
function updates() {
$.getJSON("fetch.php", function(data) {
$.each(data, function (index, item) {
var eachrow = "<tr>"
+ "<td>" + item[1] + "</td>"
+ "<td>" + item[2] + "</td>"
+ "<td>" + item[3] + "</td>"
+ "<td>" + item[4] + "</td>"
+ "</tr>";
$('#tbody').append(eachrow);
});
});
}
PHP腳本
<?php
include "./pdoConn.php";
$output = array();
$query = "select * from wishers";
$stmt = $pdo->query($query);
$stmt->execute();
$name = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($name);
?>
輸出JSON的:
[
{"id":"1","name":"Tom","password":"098f6bcd4621d373cade4e832627b4f6"},
{"id":"2","name":"Jerry","password":"098f6bcd4621d373cade4e832627b4f6"},
{"id":"3","name":"Kate","password":"098f6bcd4621d373cade4e832627b4f6"},
{"id":"4","name":"Joan","password":"098f6bcd4621d373cade4e832627b4f6"},
{"id":"5","name":"Cyril","password":"098f6bcd4621d373cade4e832627b4f6"},
{"id":"8","name":"Ama","password":"ama"},
{"id":"7","name":"Akusika","password":"mummy"},
{"id":"9","name":"Abetiafa","password":"joko"}
]
您可以嘗試:https://json2html.herokuapp.com/ 我開發了這個應用程序(開源)。 JSON 2 HTML解析器是用Python編寫的。嘗試一下,代碼在https://github.com/softvar/json2html-flask – softvar