【发布时间】:2020-03-19 08:02:58
【问题描述】:
我想在 php 中解析这个 json 并显示在一个表格中。
{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}
【问题讨论】:
标签: php
我想在 php 中解析这个 json 并显示在一个表格中。
{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}
【问题讨论】:
标签: php
你可以像这样使用简单的 foreach 循环。
<?php
$json = '{"Files":[{"name":"Tester","Dir":true,"path":"/stor/ok"},{"name":"self","Dir":true,"path":"/stor/nok"}]}';
$json = json_decode($json, true);
?>
<!DOCTYPE html>
<html>
<body>
<table border="1">
<tr><td>name</td><td>Dir</td><td>path</td></tr>
<?php foreach ($json["Files"] as $k => $v): ?>
<tr>
<td><?php echo htmlspecialchars($v["name"]); ?></td>
<td><?php echo htmlspecialchars($v["Dir"]); ?></td>
<td><?php echo htmlspecialchars($v["path"]); ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
【讨论】:
我为您创建了一些小代码示例。您将字符串解码为 json 数组。此后,您可以使用 foreach 循环解析 Files 数组。然后在 foreach 中,您可以输出/保存您的值。在这种情况下,我输出name。
$string = '{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}';
$string = json_decode($string, true);
if ($string != null)
{
foreach ($string['Files'] as $values)
{
echo $values['name'];
echo "\n";
}
}
输出:
测试人员
自我
【讨论】: