【发布时间】:2013-11-30 16:35:01
【问题描述】:
我正在动态接收 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"
}
]
}
所以,我尝试了以下代码(仅用于演示目的):
<?php
$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 时为什么它不起作用?
【问题讨论】: