我只需要显示JSON编码对象的一部分

时间:2015-08-11 11:26:50

标签: php json object

您好我是PHP的新手,但已经设法在我的要求中达到了这个目标:

我试图只显示JSON解码对象的一部分。我调用了对象$Results

我可以成功使用var_dump ($Results);,然后按如下方式获得完整结果:

object(stdClass)[2]
  public '0' => 
    object(stdClass)[3]
      public 'forename_1' => string 'JAMES' (length=5)
      public 'middle1_1' => string '' (length=0)
      public 'middle2_1' => string '' (length=0)
      public 'middle3_1' => string '' (length=0)
      public 'surname_1' => string 'TURNER' (length=7)      
  public 'Status' => int 100

然后我使用以下代码将其插入表中:

<html>
<form id="client-details" action="/details.php" method="post">
  <table>
    <thead>
        <tr> 
            <th>First Name</th>
            <th>Surname</th>
             <th>Search</th>  
        </tr>
     </thead>
<?php foreach($Results as $o):?>
<tr>
  <td id="forename"><?= $o->forename_1 ?></td>
  <td id="surname"><?= $o->surname_1 ?></td>
  <td><button type="submit" >More Info</button></td>
</tr>
<?php endforeach; ?>
</table></form>
</html>

继承问题 当我显示结果时,我收到以下错误: &#34;注意:试图获得非对象的属性..&#34;

这似乎是因为我试图运行对象的public 'Status' => int 100部分。

所以我的问题是: 我如何阻止该表尝试填充该状态&#39;或者如何完全忽略它?

编辑: 如果我想,我可以将json_decode的结果作为关联数组而不是对象...这会帮助我忽略“状态”#39;阵列/对象?

4 个答案:

答案 0 :(得分:3)

我认为你弄错了。你正在做的是迭代对象的所有变量,即首先得到公共变量0(也是一个对象),在第二次运行语句foreach中你获得变量Status并且因为&#39的值;状态&#39;是int并且没有名为&#39; forename_1&#39;等等,你得到属性不存在的错误。

如果您真的希望这个工作,您必须更改JSON对象的结构,以便您可以遍历要显示的人员列表,例如:

object(stdClass)[2]
  public 'list' => 
    array(0 => 
        object(stdClass)[3]
          public 'forename_1' => string 'JAMES' (length=5)
          public 'middle1_1' => string '' (length=0)
          public 'middle2_1' => string '' (length=0)
          public 'middle3_1' => string '' (length=0)
          public 'surname_1' => string 'TURNER' (length=7)      
          public 'Status' => int 100,
        1 => 
        object(stdClass)[3]
          public 'forename_1' => string 'JAMES' (length=5)
          public 'middle1_1' => string '' (length=0)
          public 'middle2_1' => string '' (length=0)
          public 'middle3_1' => string '' (length=0)
          public 'surname_1' => string 'TURNER' (length=7)      
          public 'Status' => int 100,
        2 => 
        object(stdClass)[3]
          public 'forename_1' => string 'JAMES' (length=5)
          public 'middle1_1' => string '' (length=0)
          public 'middle2_1' => string '' (length=0)
          public 'middle3_1' => string '' (length=0)
          public 'surname_1' => string 'TURNER' (length=7)      
          public 'Status' => int 100
    )

编辑:

如果您不能或不想更改数据结构,则将函数调用json_decode的结果作为关联数组,然后在foreach语句中检查是否存在必需字段:

$Result = json_decode($data, true);

<?php foreach($Results as $o):?>
    <?php if(isset($o['forename_1']) && isset($o['surname_1'])): ?>
        <tr>
          <td id="forename"><?= $o['forename_1'] ?></td>
          <td id="surname"><?= $o['surname_1'] ?></td>
          <td><button type="submit" >More Info</button></td>
        </tr>
    <?php endif; ?>
<?php endforeach; ?>

答案 1 :(得分:1)

我找到了一个可接受的解决方案! 在我的json_decode行中添加true

$Results = json_decode($serviceResponse, true);

我将结果作为关联数组而不是对象返回。

然后我更改了每个<tr>内的代码,如下所示:

<html>
.... (rest of code here)
<?php foreach($Results as $person):?>
         <tr>
           <td id="forename"><?= $person['forename_1'] ?></td>
           <td id="surname"><?= $person['surname_1'] ?></td>
           <td><button type="submit" >More Info</button></td>
         </tr>
<?php endforeach; ?>
     </table>
   </form>
</html>

所以现在发生的事情是返回错误的最后一行'status'现在只是一个空行。 虽然这不是一个完美的解决方案,但当我将页面视为HTML时,我不介意处理空表行。

答案 2 :(得分:0)

试试这个:

<html>
    <form id="client-details" action="/details.php" method="post">
      <table>
        <thead>
            <tr> 
                <th>First Name</th>
                <th>Surname</th>
                 <th>Search</th>  
            </tr>
         </thead>
    <?php foreach($Results as $o=>$v){?>
    <tr>
      <td id="forename"><?= $v->forename_1 ?></td>
      <td id="surname"><?= $v->surname_1 ?></td>
      <td><button type="submit" >More Info</button></td>
    </tr>
    <?php } ?>
    </table></form>
    </html>

答案 3 :(得分:0)

尝试像这样打印:

<?php foreach($Results as $o):?>
<tr>
  <td id="forename"><?= $o['forename_1']; ?></td>
  <td id="surname"><?= $o['surname_1'] ?></td>
  <td><button type="submit" >More Info</button></td>
</tr>
<?php endforeach; ?>