我正在动态地接收一个
JSON字符串,看起来像:
{ “post”: [ { “id”: “11”, “body”: “”, “image”: “images/rose.png”, “stamp”: “2013-11-04 14:50:11” } ] }
我试图漂亮地打印这个JSON字符串,如下所示:
{
"post": [
{
"id": "11",
"body": "",
"image": "images/rose.png",
"stamp": "2013-11-04 14:50:11"
}
]
}
所以,我尝试了以下代码(仅用于演示目的):
$str = '{ "post": [ { "id": "11", "body": "", "image": "images\/rose.png", "stamp": "2013-11-04 14:50:11" } ] }';
$obj = json_decode($str);
echo json_encode($obj, JSON_PRETTY_PRINT);
它只输出未格式化的JSON字符串:
{ “post”: [ { “id”: “11”, “body”: “”, “image”: “images/rose.png”, “stamp”: “2013-11-04 14:50:11” } ] }
但是当我在json_encode()语句之上添加以下行时,它的工作原理如预期.
header('Content-Type: text/plain');
可能导致这个问题?当Content-Type为text / html时为什么不起作用?