我正在使用JSON数据检索表格式数据到PHP

时间:2017-12-09 08:41:37

标签: php html json

我正在尝试通过将JSON数据调用到PHP代码中来将数据转换为表格格式。

编写用于转换为表格格式的JSON代码。

[

  {
   "#":"3586 "
   "Project" :"SSMT",
   "Tracker" :"Maintenance",
   "Status" : "To Do"
   "Subject" : "Test the following scenario and provide the relevant scripts in centos7"
   "Author" : "Anil K"
   "Assignee" : "SSMT Group"
  },
  {
   "#" :"3517"
   "Project" : "SSMT"
   "Tracker" : "Improvement"
   "Status" : "In Progress"
   "Subject" : "Image server daily backup"
   "Author" : "Lakshmi G"
   "Assignee" : "Pooja S"
  },
  {
   "Project" : "SSMT"
   "Tracker" : "Improvement"
   "Status" : "In Progress"
   "Subject" : "setup openstack all-in-one in centos7 instance on ORAVM1 box."
   "Author" : "Anil K"
   "Assignee" : "Bhanuprakash P"
  }
]

编写以下php代码以获取上述JSON数据。

<!DOCTYPE html>
<html lang = "en-US">

<?php

$url = 'data.json';
$data = file_get_contents($url);
$characters = json_decode($data);

echo $characters[0]->name;

foreach ($characters as $character) {
         echo $character->name . '<br>';

}
?>

 <table>
        <tbody>
                <tr>
                        <th>#</th>
                        <th>Project</th>
                        <th>Tracker</th>
                        <th>Status</th>
                        <th>Subject</th>
                        <th>Author</th>
                        <th>Assignee</th>

                </tr>
                        <?php
                        foreach ($characters as $character) {
                                echo '<tr>'
                                echo '<td>' . $character-># . '</td>';
                                echo '<td>' . $character->project . '</td>';
                                echo '<td>' . $character->tracker . '</td>';
                                echo '<td>' . $character->status . '</td>';
                                echo '<td>' . $character->subject . '</td>';
                                echo '<td>' . $character->author . '</td>';
                                echo '<td>' . $character->assignee . '</td>';
                                echo '</tr>';
                        }
                        ?>
        </tbody>

 </table>

</html>

我收到了以下错误 PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /var/www/html/tabularformat.php on line 34

任何人都可以建议othis? ?

添加后;在每个'echo'语句的末尾。也得到了同样的错误。请找到它。

1 个答案:

答案 0 :(得分:0)

  • 我建议使用true作为第二个参数来形成数组元​​素。
  • 你的json无效(我在我的代码中修复了它)
  • 您需要区分大小写访问密钥。
  • 在尝试访问之前检查元素是否存在。

代码:(Demo

$json='[
  {
   "#":"3586 ",
   "Project" :"SSMT",
   "Tracker" :"Maintenance",
   "Status" : "To Do",
   "Subject" : "Test the following scenario and provide the relevant scripts in centos7",
   "Author" : "Anil K",
   "Assignee" : "SSMT Group"
  },
  {
   "#" :"3517",
   "Project" : "SSMT",
   "Tracker" : "Improvement",
   "Status" : "In Progress",
   "Subject" : "Image server daily backup",
   "Author" : "Lakshmi G",
   "Assignee" : "Pooja S"
  },
  {
   "Project" : "SSMT",
   "Tracker" : "Improvement",
   "Status" : "In Progress",
   "Subject" : "setup openstack all-in-one in centos7 instance on ORAVM1 box.",
   "Author" : "Anil K",
   "Assignee" : "Bhanuprakash P"
  }
]';

$characters = json_decode($json,true);
foreach ($characters as $character) {
    echo '<tr>';
        echo '<td>',(isset($character['#'])?$character['#']:''),'</td>';
        echo '<td>',(isset($character['Project'])?$character['Project']:''),'</td>';
        echo '<td>',(isset($character['Tracker'])?$character['Tracker']:''),'</td>';
        echo '<td>',(isset($character['Status'])?$character['Status']:''),'</td>';
        echo '<td>',(isset($character['Subject'])?$character['Subject']:''),'</td>';
        echo '<td>',(isset($character['Author'])?$character['Author']:''),'</td>';
        echo '<td>',(isset($character['Assignee'])?$character['Assignee']:''),'</td>';
    echo '</tr>';
}

输出:

<tr>
    <td>3586 </td>
    <td>SSMT</td>
    <td>Maintenance</td>
    <td>To Do</td>
    <td>Test the following scenario and provide the relevant scripts in centos7</td>
    <td>Anil K</td>
    <td>SSMT Group</td>
</tr>
<tr>
    <td>3517</td>
    <td>SSMT</td>
    <td>Improvement</td>
    <td>In Progress</td>
    <td>Image server daily backup</td>
    <td>Lakshmi G</td>
    <td>Pooja S</td>
</tr>
<tr>
    <td></td>
    <td>SSMT</td>
    <td>Improvement</td>
    <td>In Progress</td>
    <td>setup openstack all-in-one in centos7 instance on ORAVM1 box.</td>
    <td>Anil K</td>
    <td>Bhanuprakash P</td>
</tr>

否则,您可以使用“封装”来访问#对象。

$characters = json_decode($json);
foreach ($characters as $character) {
    echo '<tr>';
        echo '<td>',(isset($character->{'#'})?$character->{'#'}:''),'</td>';
        // ...

来自the manual

  

示例#2访问无效对象属性

     

通过将元素名称封装在大括号和撇号中,可以访问包含PHP命名约定下不允许的字符的对象中的元素(例如连字符)。

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345