我有这个代码:
<?php
header('Content-Type: text/javascript; charset=UTF-8');
header('Cache-Control: private, no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: Sat, 01 Jan 2000 00:00:00 GMT');
$data = array(
"data" => array(
"sender" => "Jhon Andrew",
"recipient" => "Someone OverThe Internet",
"conversation" =>
array(
"unix" => "1234567890",
"message" => "Lorem ipsum dolor sit amet."
),
array(
"unix" => "0987654321",
"message" => "Tema tis rolod muspi merol."
)
)
);
echo json_encode($data);
?>
我期待这样的结果:
{
"data": {
"sender":"Jhon Andrew",
"recipient":"Someone OverThe Internet",
"message":"Lorem ipsum dolor sit amet."
}
}
但我只显示在一行中,如下所示:
{"data":{"sender":"Jhon Andrew","recipient":"Someone OverThe Internet","message":"Lorem ipsum dolor sit amet."}}
我怎样才能得到正确格式化的 JSON 输出,就像我期望的那样?实际上这并不重要,但我只想以良好的格式查看结果。
...顺便说一句,我刚刚从 facebooks 图表链接复制了标题,因为这就是我想要输出结果的方式。示例:graph.facebook.com/mOngsAng.gA
它当然是有效的。我只想知道如何像这样输出它:graph.facebook.com/mOngsAng.gA - 如您所见,它的格式正确。我的意思是它有换行符和缩进。不像我得到的只是显示在一行中。