如何在php中解析这个json文件?
我想用php解析这个json,并在表中显示它如何在php中解析这个json文件?,php,Php,我想用php解析这个json,并在表中显示它 {"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]} 我为您创建了一些小代码示例。将字符串解码为json数组。此后,您可以使用foreach循环解析文件数组。然后在foreach中,您可以输出/保存您的值。在本例中,我输出名称 $string = '{"Files":[{"name":"
{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}
我为您创建了一些小代码示例。将字符串解码为json数组。此后,您可以使用
foreach
循环解析文件
数组。然后在foreach中,您可以输出/保存您的值。在本例中,我输出名称
$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";
}
}
输出:
测试仪self您可以像这样使用简单的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>