有没有解决方案来防止json_encode添加转义字符?我从ajax请求返回一个json obj。防止json_encode添加转义字符
这是我目前有:
foreach ($files as $file)
{
$inf = getimagesize($file);
$farr[] = array (
"imgurl" => "/".str_replace("\\" , "/" , str_replace(DOCROOT , "" , $file)) ,
"width" => $inf[0] ,
"height" => $inf[1]
);
}
$t = json_encode($farr);
它提供:
[
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/96\\\/full.png\",\"width\":580,\"height\":384},
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/95\\\/full.png\",\"width\":580,\"height\":452},
{\"imgurl\":\"\\\/_assets\\\/portfolio\\\/94\\\/full.png\",\"width\":580,\"height\":384}
]
,但我需要:具有援引导致了该imgUrl的宽度和高度指标
[
{imgurl:"/_assets/portfolio/96/full.png",width:580,height:384},
{imgurl:"/_assets/portfolio/95/full.png",width:580,height:452},
{imgurl:"/_assets/portfolio/94/full.png",width:580,height:384}
]
休息我的JavaScript打破
运气不好,所以任何提示非常欢迎...
'json_encode()'做的是正确的事情。这意味着你一定在做错事。请显示打破的代码。 – Tomalak 2011-05-19 15:23:53
你为什么要这样? – 2011-05-19 15:24:36
你想要的输出是一个Javascript表达式,而不是JSON序列化。然而,在你的输出中有太多的addslashes,json_encode不会这样做。 – mario 2011-05-19 15:31:09