json数据显示格式使用php


json data display format using php

我得到了如下json响应,我希望使用php以清晰的格式显示。

json响应:

{"id":"1001","subject":"English","month":"October","group":{"native":"60","others":"40"},"status":Yes}"

我希望这样显示:

<table border="1">
<tr>
<td>Id</td><td>Subject</td><td>Month</td>
</tr>
<tr><td>10001</td><td>English</td><td>October</td></tr>
<tr>
<td>Group:</td><tr><td>Native</td><td>60</td><td>Others</td><td>40</td></tr>
</tr>
<tr>
<td>Status</td><td>Yes</td>
</tr>
</table>

您可以使用json_decode()将json编码的字符串转换为PHP变量。

如果您将assoc标志设置为true,此函数将以StdObject或简单数组的形式返回示例json。

一旦你有了,你可以简单地使用相关的键将值插入到HTML表中:

$json='{"id":"1001","subject":"English","month":"October","group":{"native":"60","others":"40"},"status":Yes}';
$phpArray=json_decode($json,true)

HTML视图:

<tr><td><?php echo $phpArray['id']?></td><td><?php echo $phpArray['subject']?></td><td><?php echo $phpArray['month']?></td></tr>

请参阅:http://php.net/manual/en/function.json-decode.php

使用

$jsonObject = json_decode( $jsonResponse );

你会得到:

object(stdClass)#1 (5) { ["id"]=> int(1001) ["subject"]=> string(7) "English" ["month"]=> string(7) "October" ["group"]=> object(stdClass)#2 (2) { ["native"]=> int(60) ["others"]=> int(40) } ["status"]=> string(3) "Yes" } 

下一个示例:echo $jsonObject->subject;