示例如下:https://graph.facebook.com/19292868552
这怎么可能?页面的来源就像页面显示的那样!没有任何形式的规则。如果你试图改变url的最后2个数字,你可以得到其他的例子。他们如何在数组的每个元素之后用回车返回这个JSON而没有任何类型的<br/>
?
在php.net中,我找不到任何像这样漂亮地打印JSON的方法。
$vettore = array(
"uno" => 1,
"due" => 2,
"tre" => 3
);
header("Content-Type: text/javascript; charset=UTF-8");
echo json_encode($vettore, JSON_PRETTY_PRINT);
如果您查看回复头,有这一行:
Content-Type: text/javascript; charset=UTF-8
这样,浏览器就知道这个页面不是HTML,并且不会尝试这样格式化它。
告诉浏览器这是纯文本,然后打印出来。
header('Content-type: text/plain');
尽管他们实际上使用text/javascript
,如页面属性所示。
您可以这样做,这似乎不是实现整齐格式化Json的最佳方式,但它确实有效。
$json_stuff = array(
"url" => "http://example.com",
"name" => "my name",
"description" => "some description",
"other" => "some text"
);
foreach ($json_stuff as $key => $val) {
$json_string .= "'"${key}'" : '"${val}'",'n";
}
header('Content-Type: application/json');
die("{'n".rtrim($json_string, ",'n")."'n}");
上面的url将输出下面的url,并带有换行符和不带反斜杠。
{
"url" : "http://example.com",
"name" : "my name",
"description" : "some description",
"other" : "some text"
}