如果你是程序员php读取json数据并以表格形式显示
假设我们有一个名为"data.json"的文件,其中包含以下内容:
{
"students": [
{
"name": "Tom",
"age": 18,
"gender": "male"
},
{
"name": "Lucy",
"age": 20,
"gender": "female"
},
{
"name": "Jack",
"age": 19,
"gender": "male"
}
]
}
以下是一个简单的PHP代码,可以读取该JSON数据并将其呈现为HTML表格:
<?php
$json_data = file_get_contents('data.json');
$data = json_decode($json_data, true);
echo '<table>';
echo '<tr><th>Name</th><th>Age</th><th>Gender</th></tr>';
foreach ($data['students'] as $student) {
echo '<tr>';
echo '<td>'.$student['name'].'</td>';
echo '<td>'.$student['age'].'</td>';
echo '<td>'.$student['gender'].'</td>';
echo '</tr>';
}
echo '</table>';
?>
在上面的代码中,我们使用了"file_get_contents()"函数来读取"data.json"文件中的JSON数据,并使用"json_decode()"函数将其解码为PHP数组。接下来,我们使用一个循环来遍历该数组,并在每次迭代中将数据呈现为HTML表格中的一行。最后,我们将整个表格呈现出来。
原文地址: https://www.cveoy.top/t/topic/bb3L 著作权归作者所有。请勿转载和采集!