【问题标题】:How can i parse this json file in php? [duplicate]我如何在 php 中解析这个 json 文件? [复制]
【发布时间】:2020-03-19 08:02:58
【问题描述】:

我想在 php 中解析这个 json 并显示在一个表格中。

{"Files":[{"name":"Tester","Dir":true,"path":"\/stor\/ok"},{"name":"self","Dir":true,"path":"\/stor\/nok"}]}

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以像这样使用简单的 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>
    

    结果

    【讨论】:

      【解决方案2】:

      我为您创建了一些小代码示例。您将字符串解码为 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";
          }
      }
      

      输出:

      测试人员
      自我

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 2012-05-24
        • 2014-04-16
        • 2019-10-02
        相关资源
        最近更新 更多